Skip to content

Instantly share code, notes, and snippets.

@Logan676
Created June 11, 2015 02:49
Show Gist options
  • Save Logan676/f70abf8bd892ec849010 to your computer and use it in GitHub Desktop.
Save Logan676/f70abf8bd892ec849010 to your computer and use it in GitHub Desktop.
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.…
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment