简体   繁体   English

如何在sqlite中更新表?

[英]How to update table in sqlite?

I have two table first is "TABLE_SUBJECT" and second is "TABLE_CHAPTER".Now i want to 我有两个表首先是“TABLE_SUBJECT”,第二个是“TABLE_CHAPTER”。现在我想
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. 请给我提示或参考。

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);
}

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);

}

You can't delete columns in SQLite. 您无法删除SQLite中的列。 There is a workaround descriped here in the FAQ . 常见问题解答中有一个解决方法。

To add aa 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. 这不是一个好方法,因为您丢失了数据库中的所有数据。

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,如果不是简单地更改数字),而不是增加数据库版本:

@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. 这种方法是可扩展的,因为当有新的更改时,您可以简单地添加案例2,案例3 ...不要在案例结尾添加中断。 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. 这样,如果应用更新将版本增加到3并且更新的应用仍然在1(可能它错过了更新),则应用所有更改。

Here you can find the documentation for alter table. 在这里,您可以找到alter table的文档。 The sqlite site (sqlite.org) is in general a great help to find this kind of things. sqlite站点(sqlite.org)通常对查找这类内容非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM