Hi I have a notepad application on play store and has users data ... the app without those new changes can update , export& import backup and runs perfectly with no problem. but when I changed my old SQLiteOpenHelper arrangement , now it doesn't do anything that I mentioned above except if I rechanged back those new changes to the old arrangement .
old SQLiteOpenHelper arrangement
static final String DATABASE_NAME = "note.db";
static final String TABLE_NOTE = "tb_note";
static final String KEY_ID_NOTE = "id";
private static final String KEY_TITLE_NOTE = "title";
private static final String KEY_CONTENT_NOTE = "content";
private static final String KEY_LAST_MODIFIED_NOTE = "last_modified";
private static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
"," + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'" +
")";
private static final int DATA_VERSION = 2;
But I changed one COLUMN name in the app and my new SQLiteOpenHelper became like this which everything is the same except I changed those lines in bold below this code
static final String DATABASE_NAME = "note.db";
static final String TABLE_NOTE = "tb_note";
static final String KEY_ID_NOTE = "id";
private static final String KEY_TITLE_NOTE = "title";
private static final String KEY_CONTENT_NOTE = "content";
private static final String KEY_UPDATE_TIME = "update_time";
private static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
"," + KEY_UPDATE_TIME + " TEXT NOT NULL" +
")";
private static final int DATA_VERSION = 2;
they were like this
private static final String KEY_LAST_MODIFIED_NOTE = "last_modified";
"," + KEY_LAST_MODIFIED_NOTE + " TEXT DEFAULT \'\'" +
but now
private static final String KEY_UPDATE_TIME = "update_time";
"," + KEY_UPDATE_TIME + " TEXT NOT NULL" +
finally here is my onupgrade code
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// update database for database version < 2
if (oldVersion < 2) {
db.execSQL("ALTER TABLE " + TABLE_NOTE + " ADD COLUMN " + KEY_UPDATE_TIME + " TEXT DEFAULT \'\'");
}
}
Now How to migrate this exsiting Sqlite database with the new one without losing any data?