Wie der Titel schon sagt, habe ich eine produktive Android-App mit etwa 1000 Installationen. Ich musste eine DB-Änderung in SQLite vornehmen. Bis zu diesem Zeitpunkt war die Version der SQLite-DB auf Version "1" eingestellt.
Hoffentlich erkläre ich den Code unten ausreichend in den Kommentaren, dieser Code befindet sich in meiner SQLiteOpenHelper-Klasse, so dass die onUpgrade-Methode Teil der Klasse ist:
// Provides an upgrade path for the DB when the apps version is updated.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// First version of the DB was 1. Logic: each if statement will
// alter the DB cumulatively based on the version code. So, if the
// newVersion was version 3, there would be two if statements, one
// for oldVersion 1 and one for oldVersion 2. oldVersion 2 will
// contain the logic for upgrading from version 2 to 3, while
// oldVersion 1 will contain a combination of alter statements
// allowing the database to upgrade from version 1 directly to
// version 3.
if (oldVersion == 1) {
db.execSQL("ALTER TABLE plans ADD COLUMN " + App.CURRENCYCODE
+ " TEXT");
Locale locale = Locale.getDefault();
ContentValues content_values = new ContentValues();
content_values.put(App.CURRENCYCODE, locale.toString());
db.update(App.DBPLANS, content_values, App.ID + " > ?", new String[] {
"0"
});
}
if (oldVersion == 2) {
// Placeholder for next database upgrade instructions.
}
}
Bitte lassen Sie mich wissen, ob es hier irgendwelche Fallstricke gibt. Bisher wurde es gut getestet, obwohl ich sehr besorgt bin, mein erstes DB-Upgrade zu vermasseln. Ich habe 1.000 Benutzer oder so, ich würde es hassen, sie alle zu verlieren.
Nochmals vielen Dank!