I am creating an app that needs a database. I created it using sqlite database browser, which means the app I created, imports the database I created into the phone.
The
You should add some code into the onUpgrade
method. With that, you can check the oldVersion and the newVersion and do the proper ALTER TABLE statements. As you can see, the current version is 23 and the check code checks what is the old version. If version 22 it does just the v22 statements, but if version 21 it does both v21 AND v22 statements. This is part of the Google I/O app:
private static final int VER_LAUNCH = 21;
private static final int VER_SESSION_FEEDBACK_URL = 22;
private static final int VER_SESSION_NOTES_URL_SLUG = 23;
private static final int DATABASE_VERSION = VER_SESSION_NOTES_URL_SLUG;
...
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);
// NOTE: This switch statement is designed to handle cascading database
// updates, starting at the current version and falling through to all
// future upgrade cases. Only use "break;" when you want to drop and
// recreate the entire database.
int version = oldVersion;
switch (version) {
case VER_LAUNCH:
// Version 22 added column for session feedback URL.
db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "
+ SessionsColumns.SESSION_FEEDBACK_URL + " TEXT");
version = VER_SESSION_FEEDBACK_URL;
case VER_SESSION_FEEDBACK_URL:
// Version 23 added columns for session official notes URL and slug.
db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "
+ SessionsColumns.SESSION_NOTES_URL + " TEXT");
db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "
+ SessionsColumns.SESSION_SLUG + " TEXT");
version = VER_SESSION_NOTES_URL_SLUG;
}
Log.d(TAG, "after upgrade logic, at version " + version);
if (version != DATABASE_VERSION) {
Log.w(TAG, "Destroying old data during upgrade");
db.execSQL("DROP TABLE IF EXISTS " + Tables.BLOCKS);
// ... delete all your tables ...
onCreate(db);
}
}