升级sqlite db的正确方法是什么?

时间:2014-04-15 08:40:57

标签: android database sqlite

private static final String TABLE_MAIN_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_MAIN_NAME + " ( a INTEGER, b LONG, c TEXT, d TEXT, e DATETIME, f TEXT)";
private static final String TABLE_MAIN_UPGRADE = "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column f TEXT";

    @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_MAIN_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < newVersion) {
        db.execSQL(TABLE_MAIN_UPGRADE);
    }
    onCreate(db);
}

之前的db版本没有&#34; f&#34;领域。 这是升级数据库的正确方法吗? 我是否需要onUpgrade中的onCreate(db)?

我这样做了几次,每次我得到另一个例外: 1)表已经存在所以我添加了#34; IF NOT EXISTS&#34; 2)无法读取第0行,第5列错误,我丢失了它....

这是升级db的正确方法吗?

我想保留我的数据

4 个答案:

答案 0 :(得分:0)

您无需在onUpgrade中调用onCreate。执行db.execSQL(TABLE_MAIN_UPGRADE)以进行更新。 现在您应该清除应用程序的数据。要做到这一点,请重新安装应用程序或在android shell中执行

pm clear your.app.package

答案 1 :(得分:0)

onUpgrade方法中可以做的一种方法是删除表并重新创建它。这样,只删除该特定表的数据,而不删除整个DB数据。

@Override  
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    Log.w("TAG", "Upgrading database from version " + oldVersion + " to " + newVersion);
    switch (oldVersion) 
    {
        case 1:
               db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAIN_NAME);
               db.execSQL(TABLE_MAIN_CREATE);
        default:
            break;
    }
}

答案 2 :(得分:0)

Android Documentation:

  

在配置数据库连接之后以及根据需要创建,升级或降级数据库模式之后调用此方法。

升级数据库时不需要

onCreate(db);

答案 3 :(得分:-11)

警告:这是一个很好且有效的解决方案,但是有很多人因为他们有个人的or because they don't know java's basics(case from 10 to 12 are without break;)

而拒绝这个答案。

你必须做某事取决于oldVersion

fx:你可以拥有10-14的db版本,10岁以上的旧版本你不支持wana支持,并且在每个新版本中添加一个代码应该看起来像新的列(newVersion是14):

private static final String TABLE_MAIN_CREATE_14 = 
    "CREATE TABLE IF NOT EXISTS " + TABLE_MAIN_NAME + 
    " ( a INTEGER, b LONG, c TEXT, d TEXT, e DATETIME, f TEXT, g TEXT, h TEXT, i TEXT)";
private static final String TABLE_MAIN_UPGRADE_10_11 = 
    "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column f TEXT";
private static final String TABLE_MAIN_UPGRADE_11_12 = 
    "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column g TEXT";
private static final String TABLE_MAIN_UPGRADE_12_13 = 
    "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column h TEXT";
private static final String TABLE_MAIN_UPGRADE_13_14 = 
    "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column i TEXT";
    @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_MAIN_CREATE_14);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch(oldVersion){
            case 10: // from 10 to 11 and go to next case
                db.execSQL(TABLE_MAIN_UPGRADE_10_11);
            case 11: // from 11 to 12 and go to next case
                db.execSQL(TABLE_MAIN_UPGRADE_11_12);
            case 12: // from 12 to 13 and go to next case
                db.execSQL(TABLE_MAIN_UPGRADE_12_13);
            case 13: // from 13 to newVersion
                db.execSQL(TABLE_MAIN_UPGRADE_13_14);
                break;
            default:
                //not upgratable too old - so we should drop and recreate;
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAIN_NAME  );
                onCreate(db);
                break;
    }
}