Java 如何更新sqlite中的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9821015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update table in sqlite?
提问by Sandip Armal Patil
I have two table first is "TABLE_SUBJECT" and second is "TABLE_CHAPTER".Now i want to
add some column and delete previous. My question is that how to do that. i try to update but
it display previous not new. I need to delete previous and update table with new column.
I change version number but it not working. Please give me hint or reference.
我有两个表,第一个是“TABLE_SUBJECT”,第二个是“TABLE_CHAPTER”。现在我想
添加一些列并删除以前的列。我的问题是如何做到这一点。我尝试更新,但
它显示以前的不是新的。我需要删除以前的表并使用新列更新表。
我更改了版本号,但它不起作用。请给我提示或参考。
Here is my some code of SQLite:
这是我的一些 SQLite 代码:
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
database.execSQL(DATABASE_CREATE1);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(MySQLiteHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_SUBJECT);
database.execSQL("DROP TABLE IF EXISTS" + TABLE_CHAPTER);
onCreate(database);
}
采纳答案by Android
you try this code
你试试这个代码
public long update_todo_not(String a, String b, String c, String d,
String e, String f, String g, String id) {
ContentValues con = new ContentValues();
con.put("title", a);
con.put("description", b);
con.put("due_date", c);
con.put("alarm_time", d);
con.put("category", e);
con.put("alarm_set",f);
con.put("priority", g);
Log.v("priority", g+"");
return mDb.update(DATABASE_TABLE_TODO_LIST, con, "id ='" + id + "'",null);
}
回答by Gabriel Ittner
You can't delete columns in SQLite. There is a workaround descriped here in the FAQ.
您不能删除 SQLite 中的列。常见问题解答中描述了一种解决方法。
To add a a new column you could simply increase the database version and change the create strings you got to the desired database layout (remove the column you want to delete and add the other). When the database is next opened onUpgrade is called, which will delete the existing tables. After that the changed tables are created. This isn't a good method, because you loose all data in the database.
要添加新列,您可以简单地增加数据库版本并将创建的字符串更改为所需的数据库布局(删除要删除的列并添加其他列)。下次打开数据库时调用 onUpgrade ,这将删除现有表。之后创建更改的表。这不是一个好方法,因为您丢失了数据库中的所有数据。
To add a new column without loosing all data use you have to change the onUpgrade method to something like this (this anticipates that you current database version is 1, if not simply change the number at case) and than increase the database version:
要在不丢失所有数据的情况下添加新列,您必须将 onUpgrade 方法更改为类似的内容(这预计您当前的数据库版本为 1,如果不是简单地更改 case 的数字),而不是增加数据库版本:
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
database.execSQL("ALTER TABLE " +
TABLE_SUBJECT + " ADD COLUMN " + KEY_NEWCOLUMN + " text not null");
database.execSQL("ALTER TABLE " +
TABLE_CHAPTER + " ADD COLUMN " + KEY_NEWCOLUMN + " text not null");
}
}
This method is scalable, because when there are new changes you can simply add case 2, case 3... Do not add break at the end of case. This way if an app update increases the version to 3 and the updated app is still on 1 (maybe it missed an update) all changes are applied.
这种方法是可扩展的,因为当有新的变化时,您可以简单地添加案例 2、案例 3...不要在案例末尾添加中断。这样,如果应用程序更新将版本增加到 3 并且更新的应用程序仍为 1(可能它错过了更新),则会应用所有更改。
Hereyou can find the documentation for alter table. The sqlite site (sqlite.org) is in general a great help to find this kind of things.
您可以在此处找到有关 alter table 的文档。sqlite 站点 (sqlite.org) 通常对查找此类内容有很大帮助。