Reputation: 135
I am totally confused by the meaning of DB version in my DBadapter and user_version in database Pragma.
I tested using db version=4 then made a back up and opened it in my PC using DB Browser there "SQL PRAGMA user_version" returned 52!
Trying to match versions using "PRAGMA user_version=4" generate a file that would not restore.
A new test using db version=5 showed Pragma user_version 53!
Any debug variants build with db version < 4 would crash.
To use db version=1 I had to build a release variant and uninstall the debug version.
This time the backup file open in DB Browser shows a user_version 49!
Editing user_version of the above backups from 53 to 49 would restore the database fine.
Uninstalling the release variant and back to debug version I could run app with db version = 1 but user_version was still 49.
Question: how to make user_version match DB version or the other way around so to make restore possible?
Upvotes: 0
Views: 2180
Reputation: 135
I find the solution it was all due to a silly declaration in DBadapter(from website):
static final int DBVERSION = '1' ; // note single quaotes
this would set version to the ASCII value of '1' which is integer 49. Likewise '2' is 50 so PRAGMA user_value is set to 49 or 50!! Simply removing quotes and reinstalling app fixed my problem. Now db version and user_version are the same a crucial factor when restoring the db.
Upvotes: 1
Reputation: 56953
Question: how to make user_version match DB version or the other way around so to make restore possible?
To force the user_version use (before the SQLiteOpenHelper subclass has opened the database) :-
db.setVersion(version);
or via
db.execSQL("PRAGMA user_version=" + Integer.toString(version),null);
Where db is the respective SQLiteDatabase and version is an int with the DB Version.
Alternately you can override the onDowngrade
method of the SQLiteOpenHelper
subclass, this will then not result in the exception as per :-
onDowngrade added in API level 11
void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion)
Called when the database needs to be downgraded. This is strictly similar to onUpgrade(SQLiteDatabase, int, int) method, but is called whenever current version is newer than requested one. However, this method is not abstract, so it is not mandatory for a customer to implement it. If not overridden, default implementation will reject downgrade and throws SQLiteException
This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.
The user_version will then be set to the DB Version (if I recall correctly). This does assume that onUpgrade
has been implemented accordingly.
Upvotes: 0