Android的升级SQLite数据库问题 [英] Android upgrade sqlite database issue
问题描述
我有我的升级SQLite数据库有点问题。现在我用这样的:
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)");
}
}
它的工作,如果你已经从beggining..but安装的应用程序,如果你现在安装它,它只会在情况下3
,因为目前的版本上运行的查询3。
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.
我需要找到一种方式来运行从一开始查询..如果我的数据库的最后一个版本是5,我想先运行的情况下2比3的情况下,比4,最后的情况下5.所以我不需要写从一开始的查询,因为它会崩溃的老用户。
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?
推荐答案
距离的情况下2添加到查询的情况下3和每个案件之后添加break语句。所以你的情况就会是这样的:
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;
}
}
更新:不要smthg是这样的:
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;
}
}
这篇关于Android的升级SQLite数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!