问题
I have a little issue with upgrading my sqlite database. For now I'm using this :
@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)");
}
}
It's working if you already have the application installed from the beggining..but if you install it now, it will run queries only in case 3
, because the current version is 3.
I need to find a way to run the queries from the beginning.. if my last version of database is 5, I want to run first the case 2, than case 3, than 4 and at last case 5. So I won't need to write all the queries from the beginning because it will crash for the old users.
Any ideas how to achieve this?
回答1:
Just add your queries from the case 2 into the case 3 and add break statement after each case. So in your case it will be something like this:
@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;
}
}
Update: Do smthg like this:
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;
}
}
回答2:
What about to use a loop?
//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;
}
But It is not normal way. As if a user already added some alteration. And you again run that alteration then it will create exception.
回答3:
Instead of nesting a bunch of switches, which will get pretty ugly fast. I would suggest this approach which will cover all scenarios. It will update your existing db (not matter the version) to the current in a step-wise fashion: (as discussed 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;
}
}
}
回答4:
Simple,,just have your case conditions in reverse order and dont use break
example in below condition if i value is 3 it will print 3,2,1 if its 4 then 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");
}
来源:https://stackoverflow.com/questions/9109837/android-upgrade-sqlite-database-issue