I am trying to add three new table to my existing sqlite db and I'm running into problems with the db version not updating after a successful upgrade. Below is the DatabaseHelper that runs:
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, "Current db version is " + db.getVersion());
db.execSQL(ORIGINAL_DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
db.setVersion(newVersion);
Log.w(TAG, "Version after onUpgrade is " + db.getVersion());
}
}
Below is my open() function:
public VehicleExpDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
mDb = mDbHelper.getWritableDatabase();
return this;
}
Below is my close() function:
public void close() {
mDb.close();
mDbHelper.close();
}
So what happens on the first run with a higher DATABASE_VERSION is the first onUpgrade Log reads out: Old Version 1 New Version 2 db.getVersion is 1
The second Log is: Version after onUpgrade is 2
But then when the db is accessed again after the onUpgrade was ran the version number of the DB is not updated and it runs through the onUpgrade with the first log in the onUpgrade reading: Old Version 1 New Version 2 db.getVersion is 1
Then the app crashed because it tries creating a table that is already there.
I've tried not manually setting the db version in the onUpgrade as well. This didn't work either. I've also tried updating the version number by running...
db.execSQL("PRAGMA user_version = " + newVersion);
...at the end of the onUpgrade.
EDIT:
Per Aswin Kumar's suggestion I've changed my onUpgrade to backing up my existing table and dropping all table and then recreating them. This has not fix my version issue. Below is my onUpgrade and onCreate:
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, "Current db version is " + db.getVersion());
db.execSQL(ORIGINAL_DATABASE_CREATE);
db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
mOldDbContents = backupTables(db);
db.execSQL("DROP TABLE IF EXISTS " + ORIGINAL_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_1_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_2_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_3_DATABASE_CREATE);
onCreate(db);
}
Below are the sqlite statements that I use to create the tables:
private static final String ORIGINAL_DATABASE_CREATE = "create table " + VEHICLE_EXPENSE_TABLE_NAME +
" (" + KEY_ROW_ID + " integer primary key autoincrement, " +
KEY_UNIX_DATE + " integer, " +
KEY_DATE + " not null default current_date, " +
KEY_TIME + " not null default current_time, " +
KEY_DESCRIPTION + " text, " +
KEY_START_MILE + " integer, " +
KEY_END_MILE + " integer, " +
KEY_MILES + " text, " +
KEY_AMOUNT + " text, "
+ KEY_PARTY_ID + " integer)";
private static final String NEW_TABLE_1_DATABASE_CREATE = "CREATE TABLE " + PURCHASE_HISTORY_TABLE_NAME +
"(" + HISTORY_ORDER_ID_COL + " TEXT PRIMARY KEY, " +
HISTORY_STATE_COL + " INTEGER, " +
HISTORY_PRODUCT_ID_COL + " TEXT, " +
HISTORY_DEVELOPER_PAYLOAD_COL + " TEXT, " +
HISTORY_PURCHASE_TIME_COL + " INTEGER)";
private static final String NEW_TABLE_2_DATABASE_CREATE = "CREATE TABLE " + PURCHASED_ITEMS_TABLE_NAME +
"(" + PURCHASED_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " +
PURCHASED_QUANTITY_COL + " INTEGER)";
private static final String NEW_TABLE_3_DATABASE_CREATE = "CREATE TABLE " + TRIAL_LIMIT_TABLE_NAME +
"(" + TRIAL_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " +
TRIAL_PRODUCT_NAME + " TEXT, " +
TRIAL_START_DATE + " INTEGER)";
Any help would be great appreciated.
Thank you, Kevin