I need to check tables in my database before upgrade it.
I overrode SQLiteOpenHelper.onUpgrade(SQLiteDatabase db, int oldVer, int newVer)
in my own DatabaseHelper to do the work when database version increase.
I use this query on sqlite_master table to get table names:select * from sqlite_master
but cursor seems empty even if cursor.getCount()==29
(DB isn't empty and contains 29 objects)
The same query works great in othes places of my APP.
I'm testing this on emulated Android 4.4 (api 19) while my app is built with
minSdkVersion 12
targetSdkVersion 24
Any idea about this strange behaviour ?
Here is the code:
public class DatabaseHelper extends SQLiteOpenHelper {
public static DatabaseHelper getHelper(Context context) {
if (instance == null)
synchronized( DatabaseHelper.class ) {
if( instance==null )
instance = new DatabaseHelper(context.getApplicationContext());
}
return instance;
}
private DatabaseHelper(Context c) {
super(c, DB_NAME, null, 6 /* version */);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
final boolean inTransaction = db.inTransaction();
if( !inTransaction )
db.beginTransaction();
Cursor cur = null;
try {
// cur = db.query("sqlite_master", null, null, null, null, null, null); // SAME BEHAVIOUR
cur = db.rawQuery("select * from sqlite_master", null );
int count = cur.getCount(); // ASSIGN 29 TO COUNT
while( cur.moveToNext()) {
** NEVER ENTERS HERE **
}
if( !inTransaction )
db.setTransactionSuccessful();
} catch (Exception e) {
log.e(DBTAG, e.getMessage(), e);
} finally {
if( cur!=null)
cur.close();
if( !inTransaction )
db.endTransaction();
}
}
}
The code I posted was in a library module. When I moved it in the main app module the query on sqlite_master worked!
I upgraded buildToolsVersion in build.gradle from 24.0.2 to 24.0.3 in app module and library module. Then a full rebuild.
I don't know why but ... now query works either in main module or in library module!