I found the reason for my problem, see below!
In short, since the backup feature retrived an old database scheme with same version number, neither onUpdate() or onCreate() was invoked in my SQLiteHelperClass.
I'm experiencing a strange problem that I can not solve. When I run my app in an emulator, everything is ok. When I run it as a apk-debug.apk on my phone, everything is ok, but when I try to run it as a signed app on my phone, I get this error:
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: listId (code 1): , while compiling: SELECT * FROM mytable WHERE listId=100 ORDER BY id,name,conc ASC)
If I delete all the app data in settings an rerun the app, everything works in the signed app version although it was initially installed on a phone for the first time!
The mytable is created by running db.execSQL(DATABASE_CREATE_MY_TABLE ): (the column names are constants set elsewhere)
private static final String DATABASE_CREATE_MY_TABLE = "CREATE TABLE "
+ MY_TABLE + "("
+ ID + " INTEGER, "
+ LIST_ID + " INTEGER, "
+ NAME + " STRING, "
+ CONC + " REAL, "
+ "PRIMARY KEY (" + ID + "," + LIST_ID + ")"
+ ")"
The code that creates this error is the following:
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + MY_TABLE + " WHERE " + LIST_ID + "=?" + " ORDER BY " + NAME + "," + CONC + " ASC";
Cursor cursor = db.rawQuery(selectQuery, new String[] {listId});
My main problem is the colum LIST_ID which seems to not be created on the first install of the signed apk, but gets created if I delete the app-data an rerun the signed apk. Gets created every time on the emulator and dubug.apk on phone.
I've tried:
String selectQuery = "SELECT * FROM " + MY_TABLE + " WHERE " + LIST_ID + "=" + listId + " ORDER BY " + NAME + "," + CONC + " ASC";
Cursor cursor = db.rawQuery(selectQuery, null);
and modified it a little bit for debugging:
String selectQuery = "SELECT * FROM " + MY_TABLE + " WHERE " + ID + "=?" + LIST_ID + "=?" + " ORDER BY " + NAME + "," + CONC + " ASC";
Cursor cursor = db.rawQuery(selectQuery, new String[] {id, listId});
with the same error as a result. Tried different columns, but listId is the column that causes the error in all cases. But only when running the signed apk on my phone. My phone is a Samsung S6 if that means anythig.
As a debugging measure I've tried to force creation of the database before my app does anything with it with this code
SQLiteDatabase db = this.getWriteableDatabase();
which show me that the database is NOT created on first install. Actually it seems like there exists an old version of the database despite my uninstall efforts. Hence, I increased the database version and the onUpgrade() was invoked on first install!
It seems like the database is not created correctly on first install as signed apk or not removed properly with uninstall.
I can not seem to find the error in my code or elsewehere, I'm starting to go blind, but I hope someone else can see the bug or has experienced anything like this before and can push me in the right direction.