Created
November 13, 2011 19:21
-
-
Save knitfaced/1362533 to your computer and use it in GitHub Desktop.
upgrading sqlite database in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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