Skip to content

Upgrading while preserving user_version #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rossimo opened this issue Sep 12, 2012 · 4 comments
Closed

Upgrading while preserving user_version #66

rossimo opened this issue Sep 12, 2012 · 4 comments

Comments

@rossimo
Copy link

rossimo commented Sep 12, 2012

While using the SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2() method to convert a database, it seems that the user_version PRAGMA is reset to 0 in the newly converted database. This results in the SQLiteOpenHelper.onCreate() method being called upon my first attempt to use the converted database, even though the database already contains data. Is there a accessible method in the android-database-sqlcipher library that allows me to preserve the version PRAGMA to prevent onCreate() being called redundantly?

@developernotes
Copy link
Member

Hi rossimo,

The SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2 function makes use of the sqlcipher_export convenience function. It is feasible to export into a non-empty database, which could already have the user_version value set, so we wouldn't want to inadvertently change the value. In your situation it would be best to capture the current value prior to the upgrade with PRAGMA user_version; and then set the value following the upgrade.

@rossimo
Copy link
Author

rossimo commented Sep 12, 2012

I've attempted to read the user_version PRAGMA from the old database to set in the new database before calling SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(), but I'm encountering a SQLiteException with the message "file is encrypted or not a database" during the SQLiteDatabase.compileStatement() method. The following code is how I'm attempting to read the user_version before conversion.

File v1DatabaseFile= getDatabasePath( databaseName );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase( v1DatabaseFile, databasePassword, null, null );
SQLiteStatement prog = db.compileStatement( "PRAGMA user_version;" );
int version = (int) prog.simpleQueryForLong();

Also, for clarification, I'm not attempting to import tables (with data) from a v1 database into an existing v2 database, I'm trying to convert a v1 database (with data) into a new v2 database.

@developernotes
Copy link
Member

Hi rossimo,

You can actually use the getVersion and setVersion functions on a SQLiteDatabase instance. Since you would be using the new library to connect to an older 1.x database, you will also want to use a SQLiteDatabaseHook in your call to SQLiteDatabase.openOrCreateDatabase to disable the HMAC as 1.x does not support that. I've uploaded an example test here.

@rossimo
Copy link
Author

rossimo commented Sep 13, 2012

Thanks, that worked great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants