I want to upgrade my database version to 4.
I have changed version code in manifest file
and my database openhelper is
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
DTLog.w("Old Version" + oldVersion, "new Version" + newVersion);
DTLog.w("UPDATING DB", "Updating database");
if (oldVersion == 1 && newVersion == 2) {
db.execSQL("ALTER TABLE moduleRecord ADD COLUMN subtitle1 TEXT");
db.execSQL("ALTER TABLE moduleRecord ADD COLUMN subtitle2 TEXT");
db.delete("moduleRecord", null, null);
}
else if ((oldVersion == 2&&newVersion == 3)) {
db.execSQL("CREATE TABLE "
+ "moduleDesc"
+ "(deleteable INTEGER,createable INTEGER,updateable INTEGER,name TEXT,label TEXT,labelfields TEXT,fields TEXT);");
} else {
db.execSQL("DROP TABLE IF EXISTS " + "moduleRecord");
db.execSQL("DROP TABLE IF EXISTS " + "ModuleName");
db.execSQL("DROP TABLE IF EXISTS " + "EventStatus");
db.execSQL("DROP TABLE IF EXISTS " + "moduleDesc");
onCreate(db);
}
}
The error that I get is
07-31 12:01:15.267: W/System.err(632):
android.database.sqlite.SQLiteException: no such table: moduleDesc: ,
while compiling: insert into moduleDesc(deleteable,createable,updateable,name,label,labelfields,fields)
values(?,?,?,?,?,?,?)
07-31 12:01:15.267: W/System.err(632):
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-31 12:01:15.267: W/System.err(632):
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-31 12:01:15.267: W/System.err(632):
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-31 12:01:15.267: W/System.err(632):
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
can anyone tell me how to upgrade database
I believe your issue could be the if statement loop that you have. What if the user has either version 1 or version 2 of your application and wants to upgrade to the latest one, which I believe is 4.
In that case, it will simply fall through the if loop and go to the final
else, which tries to drop the tablemoduleDesc, which only gets created if the user had planned to upgrade from either version 1 or 2 to version 3.So in summary, examine your if statements and cater to all conditions.