The method onUpgrade is not getting called even if it gets the DB version greater than the current.
I verified the DB version using the following command and it is 6 currently.
sqlite> PRAGMA user_version;
6
In the constructor, I'm passing the following (version 7),
super(context, DB_NAME, null, 7);
Note: My class DBHelper extends SQLiteOpenHelper and the above line is present in the constructor of DBHelper.
SQLite version: 3.32.3
As I'm passing the new (greater) DB version, I expect the onUpgrade to be called. But the onUpgrade is never called. Am I missing any other thing?
UPDATE 1:
DBHelper.class
class DBHelper extends SQLiteOpenHelper
{
@Inject
public DBHelper(final Context context)
{
this(context, DBContracts.DATABASE_FILE_NAME);
}
DBHelper(final Context context, final String databaseFileName)
{
super(context, databaseFileName, null, DBContracts.DATABASE_VERSION);
Logger.i("APPTAG", "DBHelper constructor");
Logger.i("APPTAG", "DATABASE_VERSION : " + DBContracts.DATABASE_VERSION);
}
@Override
public void onCreate(final SQLiteDatabase db)
{
Logger.i("APPTAG", "onCreate");
// create scripts
}
@Override
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion)
{
Logger.i("APPTAG", "onUpgrade");
Logger.i("APPTAG", "oldVersion: " + oldVersion);
Logger.i("APPTAG", "newVersion: " + newVersion);
// upgrade scripts
}
@Override
public void onConfigure(final SQLiteDatabase db)
{
Logger.i("APPTAG", "onConfigure");
Logger.i("APPTAG", "DB version: " + db.getVersion());
try {
Logger.i("APPTAG", "DB path: " + db.getPath());
int version = ((Long) DatabaseUtils.longForQuery(db, "PRAGMA user_version;", null)).intValue();
Logger.i("APPTAG", "onConfigure version: " + version);
} catch (Exception e) {
Logger.e("APPTAG", "Error" + e, e);
}
super.onConfigure(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Logger.i("APPTAG", "onDowngrade");
Logger.i("APPTAG", "oldVersion: " + oldVersion);
Logger.i("APPTAG", "newVersion: " + newVersion);
super.onDowngrade(db, oldVersion, newVersion);
}
@Override
public void onOpen(SQLiteDatabase db) {
Logger.i("APPTAG", "onOpen");
Logger.i("APPTAG", "DB version: " + db.getVersion());
super.onOpen(db);
}
}
And this is the log I see,
05-04 07:47:33.021 4295 4295 I APPTAG: DBHelper constructor
05-04 07:47:33.021 4295 4295 I APPTAG: DATABASE_VERSION : 7
05-04 07:47:33.023 4295 4295 I APPTAG: UpdatesDB constructor
05-04 07:47:33.023 4295 4295 I APPTAG: SQLiteDatabaseWrapperFactory create
05-04 07:47:33.029 4295 4295 I APPTAG: onConfigure
05-04 07:47:33.030 4295 4295 I APPTAG: DB version: 7
05-04 07:47:33.030 4295 4295 I APPTAG: DB path: /data/user/0/<application-name>/databases/updates.db
05-04 07:47:33.030 4295 4295 I APPTAG: onConfigure version: 7
05-04 07:47:33.031 4295 4295 I APPTAG: onOpen
05-04 07:47:33.031 4295 4295 I APPTAG: DB version: 7
Interestingly, if I execute this command PRAGMA user_version; directly on the database, I get the version as 6. But when I tried through the code (as in onConfigure), it gives me 7. Due to this, it skips the onUpgrade method as per this code.
Could anyone help me understand why the db version is getting reported as 7 when I fetch it through code?
Clarifications:
- I'm calling
getWritableDatabase()to get the db instance. DBContracts.DATABASE_VERSIONis set to 7
I believe that your issue lies outside of the code in your question, or perhaps it is a misuse the fist constructor (perhaps comment that out and see how you fare).
Consider this modification (primarily see the end of the code and the i.e. the added
getDatabaseVersionmethod), to include retrieval of the version number prior to to opening the database and other minor changes for my convenience :-Then the following code in an activity:-
The App was run twice:-
The results being :-
Then :-
A third run, without changing anything (so db already at version 7) then :-