Android升级sqlite数据库问题

时间:2012-02-02 09:26:05

标签: android database upgrade

升级我的sqlite数据库时遇到一些问题。现在我正在使用它:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("","UPGRADE DATABASE : "+" oldVErsion : "+oldVersion+" newVersion : "+newVersion);
    switch(newVersion){
    case 2:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
    case 3:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE users ADD COLUMN firstName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN lastName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN user varchar(70)");
    }
}

如果您已经从beggining安装了应用程序,它就可以正常运行..但是如果您现在安装它,它将仅在case 3中运行查询,因为当前版本是3。

我需要找到一种从头开始运行查询的方法..如果我的最后一个版本的数据库是5,我想首先运行案例2,而不是案例3,而不是4和最后一个案例5.所以我不需要从头开始编写所有查询,因为它会为旧用户崩溃。

任何想法如何实现这一目标?

4 个答案:

答案 0 :(得分:2)

只需将案例2中的查询添加到案例3中,并在每个案例后添加break语句。所以在你的情况下,它将是这样的:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("","UPGRADE DATABASE : "+" oldVErsion : "+oldVersion+" newVersion : "+newVersion);
    switch(newVersion){
    case 2:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
        break;
    case 3:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
        db.execSQL("ALTER TABLE users ADD COLUMN firstName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN lastName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN user varchar(70)");
        break;
    }
}

更新:像这样做smthg:

switch(newVersion){
case 2:
    Log.w("","UPGRADE DATABASE : "+newVersion);
    db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
    db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
    break;
case 3:
    Log.w("","UPGRADE DATABASE : "+newVersion);
    switch(oldVersion) {
        case 1:
            db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
            db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
            db.execSQL("ALTER TABLE users ADD COLUMN firstName varchar(70)");
            db.execSQL("ALTER TABLE users ADD COLUMN lastName varchar(70)");
            db.execSQL("ALTER TABLE users ADD COLUMN user varchar(70)");
            break;
        case 2: 
            db.execSQL("ALTER TABLE users ADD COLUMN firstName varchar(70)");
            db.execSQL("ALTER TABLE users ADD COLUMN lastName varchar(70)");
            db.execSQL("ALTER TABLE users ADD COLUMN user varchar(70)");
            break;
    }

}

答案 1 :(得分:2)

如何使用循环?

       //or start = oldVersion;              
 for (int start = 2 , start<=newVersion ; start++){
      switch(start){
    case 2:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN bonus_text VARCHAR(200)");
        db.execSQL("ALTER TABLE collection_lang ADD COLUMN quantity integer");
        break;
    case 3:
        Log.w("","UPGRADE DATABASE : "+newVersion);
        db.execSQL("ALTER TABLE users ADD COLUMN firstName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN lastName varchar(70)");
        db.execSQL("ALTER TABLE users ADD COLUMN user varchar(70)");
        break;
    default:
        break;
    }

但这不正常。好像用户已经添加了一些更改。然后你再次运行那个改变,然后它会产生异常。

答案 2 :(得分:1)

而不是嵌套一堆开关,这将变得非常难看。我建议这种方法将涵盖所有方案。它将以逐步的方式将您现有的数据库(无论版本)更新为当前数据:(如所讨论的here

int curVer = oldVersion;
while ( curVer < newVersion ) {
    curVer++;
    switch ( curVer ) {
        case 2: {
            // Upgrade from V1 to V2
            break;
        }
        case 3: {
            // Upgrade from V2 to V3
            break;
        }
        case 4: {
            // Upgrade from V3 to V4
            break;
        }
    }
}

答案 3 :(得分:0)

简单,只是以相反的顺序排列你的案例条件,并且在下面的条件下不使用break 示例如果i值为3它将打印3,2,1如果它的4然后是4,3,2 ,1

switch(i){ case 4: System.out.println("4"); case 3: System.out.println("3"); case 2: System.out.println("2"); case 1: System.out.println("1"); }