Skip to content

Instantly share code, notes, and snippets.

@knitfaced
Created November 13, 2011 19:21
Show Gist options
  • Save knitfaced/1362533 to your computer and use it in GitHub Desktop.
Save knitfaced/1362533 to your computer and use it in GitHub Desktop.
upgrading sqlite database in android
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
if (newVersion > oldVersion) { // Upgrade
switch (oldVersion) {
case 1:
// Upgrade from version 1 to 2.
try {
Log.i(TAG, "upgrade 1->2: adding new column");
db.execSQL("ALTER TABLE " + DATABASE_TABLE + " ADD COLUMN " + NEW_COLUMN + " INTEGER;");
} catch (SQLException e) {
Log.e(TAG, "Error executing SQL: ", e); // "duplicate column name" error is ok
}
// fall through for further upgrades
break;
default:
Log.w(TAG, "Unknown version " + oldVersion + ". Creating new database.");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
} else {
// newVersion <= oldVersion: assume backwards compatibility
Log.w(TAG, "Don't know how to downgrade. Will not touch database and hope they are compatible.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment