jeudi 5 février 2015

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?


Variant A: // No "break" after each case, does it keep running for case 2 and 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:



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?


Variant 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);

}
}

Aucun commentaire:

Enregistrer un commentaire