you have to use onUpgrade()
method to reflect changes in local and live application.
onUpgrade()
is only called when the database file exists but the stored version number is lower than requested in constructor. The onUpgrade()
should update the table schema to the requested version.
When changing the table schema in code (onCreate()
), you should make sure the database is updated. Two main approaches:
Delete the old database file so that onCreate()
is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to to delete the database file:
Increment the database version so that onUpgrade()
is invoked. This is slightly more complicated as more code is needed.
For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>")
in to remove your existing tables and call onCreate()
to recreate the database.
For released versions, you should implement data migration in onUpgrade()
so your users don't lose their data.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
db.execSQL("ALTER TABLE " + DATABASE_TABLE + " ADD COLUMN " + NEW_COLUMN_NAME + TYPE);
}
}
Check SqlLiteOpenHelper and this explanation