[英]SQLite onUpgrade with 4 database versions
How is the proper way to execute an SQLiteOpenHelper's onUpgrade method, when we have 4 database versions and we have added a new field to the user table in each version? 当我们有4个数据库版本并且在每个版本的用户表中添加了一个新字段时,如何正确执行SQLiteOpenHelper的onUpgrade方法?
Variant A: // No "break" after each case, does it keep running for case 2 and 3? 变体A://在每种情况下都没有“中断”,它是否在情况2和3中继续运行?
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
switch (version_old) {
case 1:
database.execSQL(addPostcodeFieldToUserTable);
case 2:
database.execSQL(addGenderFieldToUserTable);
case 3:
database.execSQL(addEmailSubscriptionFieldToUserTable);
break;
}
}
Variant B: 变体B:
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
switch (version_old) {
case 1:
database.execSQL(addPostcodeFieldToUserTable);
break;
case 2:
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(addGenderFieldToUserTable);
break;
case 3:
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(addGenderFieldToUserTable);
database.execSQL(addEmailSubscriptionFieldToUserTable);
break;
}
But what do we do in the case when a user had version 1 of the DB, then missed version 2 and upgraded the app with version 3? 但是,如果用户拥有数据库的版本1,然后错过了版本2,并使用版本3升级了应用程序,我们该怎么办?
Variant 3: 变体3:
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
if(version_old==1 && current_version==2) {
database.execSQL(addPostcodeFieldToUserTable);
} else if(version_old==2 && current_version==3) {
database.execSQL(addGenderFieldToUserTable);
} else if(version_old==3 && current_version==4) {
database.execSQL(addEmailSubscriptionFieldToUserTable);
} else if(version_old==1 && current_version==3) {
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(addGenderFieldToUserTable);
} else if(version_old==1 && current_version==4) {
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(addGenderFieldToUserTable);
database.execSQL(addEmailSubscriptionFieldToUserTable);
} else if(version_old==2 && current_version==4) {
database.execSQL(addGenderFieldToUserTable);
database.execSQL(addEmailSubscriptionFieldToUserTable);
}
}
What you could also do is make the onUpgrade a recursive call, 您还可以做的是对onUpgrade进行递归调用,
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
switch (version_old) {
case 1:
database.execSQL(addPostcodeFieldToUserTable);
onUpgrade(database,version_old++,current_version);
break;
case 2:
database.execSQL(addPostcodeFieldToUserTable);
database.execSQL(addGenderFieldToUserTable);
onUpgrade(database,version_old++,current_version)
break;
default:
break;
}
u get the idea, so it will upgrade to the latest version and quit calling itself. 您知道了,因此它将升级到最新版本并退出自称。
No "break" after each case, does it keep running for case 2 and 3? 在每种情况下都没有“中断”,它是否在情况2和3中继续运行?
Yes. 是。 A common practice is to add a comment like 一种常见的做法是添加评论,例如
// fallthrough
to indicate the missing break
is intentional. 表明丢失的break
是有意的。
But what do we do in the case when a user had version 1 of the DB, then missed version 2 and upgraded the app with version 3? 但是,如果用户拥有数据库的版本1,然后错过了版本2,并使用版本3升级了应用程序,我们该怎么办?
onUprade()
would be called with oldVersion
1 and newVersion
3. The code should update the database to version 3 of your schema. onUprade()
将使用oldVersion
1和newVersion
3进行调用。代码应将数据库更新为架构的版本3。
Which variant to use depends on which is the most comfortable for you to maintain. 使用哪个变体取决于哪个最适合您维护。 I'd personally go with something like variant A since it has the least code. 我个人会使用类似A的代码,因为它的代码最少。
Using a switch
without break
s is counterintuitive. 使用switch
没有break
s是违反直觉的。
A common pattern is to use a series of if
s instead: 一个常见的模式是使用一系列if
代替:
if (version_old < 2) {
database.execSQL(addPostcodeFieldToUserTable);
}
if (version_old < 3) {
database.execSQL(addGenderFieldToUserTable);
}
if (version_old < 4) {
database.execSQL(addEmailSubscriptionFieldToUserTable);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.