I have an app and I want to update its database to the next version by adding new values to it. The new data is in their temp database.
This is my onUpgrade()
method:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// version 2
try{
db.execSQL("ALTER TABLE tbl_questions ADD COLUMN q_fav BOOLEAN DEFAULT 0");
db.execSQL("ALTER TABLE tbl_users ADD COLUMN user_voted BOOLEAN DEFAULT 0");
db.execSQL("INSERT INTO tbl_categories (_id, cat_title) VALUES (3, '\u0639\u0645\u0648\u0645\u06CC (\u0639\u0644\u0648\u0645 \u0627\u0646\u0633\u0627\u0646\u06CC)')");
db.execSQL("CREATE TABLE [tbl_schedule] (" +
"[_id] INTEGER PRIMARY KEY, " +
"[lesson_name] VARCHAR, " +
"[lesson_unit] VARCHAR, " +
"[teacher_name] VARCHAR, " +
"[exam_date] VARCHAR, " +
"[description] VARCHAR);");
db.setTransactionSuccessful();
db.endTransaction();
db.execSQL("ATTACH DATABASE '/data/data/com.andrun.payamenoo/databases/db_temp.sqlite' AS SRC;");
db.beginTransaction();
db.execSQL("INSERT INTO MAIN.tbl_questions SELECT * FROM SRC.tbl_questions;");
//db.execSQL("DETACH SRC;");
}catch(Exception e){
e.printStackTrace();
}finally{
}
super.onUpgrade(db, oldVersion, newVersion);
}
Four first work well. The problem is attaching query. Code doesn't throw any exception or any error but it also doesn't have any effect on the database and new data doesn't get added.
How do I fix this?
You must increase the number you pass in the SQLiteDatabaseOpenHelper constructor.
That is the DB_VERSION, by increasing it, your onUpgrade method will be called with the parameters being the oldVersion (= from which version) and newVersion ( = the DB_VERSION you just setted), based on these integers you can do each ALTER TABLE to all versions.