我在 Play 商店中部署了一个应用程序。我使用 SQLite 数据库。 Play 商店上的应用程序有数据库版本 3。现在对于即将到来的更新,我想在数据库中的表中添加另一列,所以我打算将数据库版本增加到 4。我已经在我的 onUpgrade( ) 方法。我只是想知道你是如何找到它的,你是如何解决这种情况的,甚至像 db 版本 5 那样的进一步更新。我只是想听听你的意见。非常感谢。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//when deployed to play store version was 3
//update db table. And increase db version on top
String queryForDBVersion4 = "ALTER TABLE " + TABLE_NAME + " ADD noteColor TEXT";
// String queryForDBVersion5;
switch (newVersion){
case 4:
db.execSQL(queryForDBVersion4);
break;
// case 5: //FOR FUTURE USE
// if(oldVersion==4){db.execSQL(queryForDBVersion5);}
// else{
// db.execSQL(queryForDBVersion4);
// db.execSQL(queryForDBVersion5);
// }
// break;
// case 6: SO ON...
}
}
最佳答案
我更喜欢这种方式,因为它简洁明了:
public void onUpgrade(final SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2){
// Upgrade from V1 to V2
}
if (oldVersion < 3){
// Upgrade from V2 to V3
}
if (oldVersion < 4){
// Upgrade from V3 to V4
}
}
使用它,数据库将逐步升级。
关于java - 在更新时升级部署的 Android 应用程序上的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646496/