Skip to content

Updating SqlCipher from 1.1 to 2.2.2 error #103

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
urspands opened this issue Sep 11, 2013 · 6 comments
Closed

Updating SqlCipher from 1.1 to 2.2.2 error #103

urspands opened this issue Sep 11, 2013 · 6 comments

Comments

@urspands
Copy link

I am trying to update the sqlcipher from version 1.1 to 2.2.2. The new version of the sqlcipher works fine in a fresh install or when i run it from eclipse. But when I update the app which was previously using v1.1 sqlcipher with new v2.2.2, the library doesn't load properly. When i call the SQLiteDatabase.loadLibs(this), the app crashes. i get the below info in the logcat

09-11 11:21:56.887: I/Database(26277): JNI_OnLoad called

09-11 11:21:56.887: I/Database(26277): JNI_OnLoad register methods

09-11 11:21:56.887: E/Database(26277): Can't find SQLiteDatabase.mNativeHandle

09-11 11:21:56.887: E/Cursor(26277): Can't find net/sqlcipher/database/SQLiteCompiledSql

09-11 11:21:56.887: E/Cursor(26277): Can't find net/sqlcipher/database/SQLiteQuery

09-11 11:21:56.887: E/Cursor(26277): Can't find net/sqlcipher/database/SQLiteProgram

09-11 11:21:56.887: E/Cursor(26277): Can't find net/sqlcipher/database/SQLiteStatement

09-11 11:21:56.887: E/CursorWindow(26277): Can't find net/sqlcipher/CursorWindow

09-11 11:21:56.903: W/dalvikvm(26277): threadid=11: thread exiting with uncaught exception (group=0x40b73930)

@developernotes
Copy link
Member

Hi urspands,

Can you verify all the new native .so files along with the new icudt46l.zip file are being included with the new application? You will have to upgrade the database format from 1x to 2x formats, but that will be a separate issue.

@urspands
Copy link
Author

Hi,
i have verified all the .so files and new icudt46l.zip file. I thought this might be something to do with proguard and then i also updated the proguard.cfg with the below code.

-libraryjars libs/commons-codec.jar
-libraryjars libs/guava-r09.jar
-libraryjars libs/sqlcipher.jar

-keep public class net.sqlcipher.** {
*;
}

-keep public class net.sqlcipher.database.** {
*;
}
But it didnt work.I turned off proguard and after that the SQLiteDatabase.loadLibs(this) worked fine. But this time I am not able to open the database. Please refer the below stack trace

09-11 14:21:48.950: I/Database(31543): sqlite returned: error code = 26, msg = file is encrypted or is not a database
09-11 14:21:48.950: E/Database(31543): CREATE TABLE android_metadata failed
09-11 14:21:48.965: E/Database(31543): Failed to setLocale() when constructing, closing the database
09-11 14:21:48.965: E/Database(31543): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102)
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1968)
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)
09-11 14:21:48.965: E/Database(31543): at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
09-11 14:21:48.965: E/Database(31543): at alc.rhapsody.mdd.database.DatabaseAdapter.openDatabase(DatabaseAdapter.java:234)
09-11 14:21:48.965: E/Database(31543): at alc.rhapsody.mdd.presenter.SplashViewPresenter.getAdmissionAccess(SplashViewPresenter.java:152)
09-11 14:21:48.965: E/Database(31543): at alc.rhapsody.mdd.presenter.SplashViewPresenter.access$2(SplashViewPresenter.java:99)
09-11 14:21:48.965: E/Database(31543): at alc.rhapsody.mdd.presenter.SplashViewPresenter$AdmissionCtrl.doInBackground(SplashViewPresenter.java:235)
09-11 14:21:48.965: E/Database(31543): at alc.rhapsody.mdd.presenter.SplashViewPresenter$AdmissionCtrl.doInBackground(SplashViewPresenter.java:1)
09-11 14:21:48.965: E/Database(31543): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-11 14:21:48.965: E/Database(31543): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-11 14:21:48.965: E/Database(31543): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-11 14:21:48.965: E/Database(31543): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-11 14:21:48.965: E/Database(31543): at java.lang.Thread.run(Thread.java:856)

@urspands
Copy link
Author

What do you mean by I need to update the database format from 1x to 2x?

@developernotes
Copy link
Member

It looks like there maybe an issue with your ProGuard configuration, could you try integrating this with your current configuration? With regard to the database format upgrade, we added a per-page HMAC to the database in 2x. To upgrade a database there is a helper function you can invoke called SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2 which accepts the database file and password and will handle the upgrade process for you.

@urspands
Copy link
Author

Hi,
I followed your suggestion and updated the proguard config and it worked fine. Now I have problem with updating the old database to new one. below is the code i use.

File dbFile = mCtx.getDatabasePath(MddDbConstants.MDD_DB_NAME);

        if (dbFile.exists()) {
            try {
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
                        dbFile, encryptionKey, null);
                int version = db.getVersion();
                if (version == 1) {
                    SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(
                            dbFile, encryptionKey);
                }
                MddLog.log(TAG, "Old Database found and updated.");
            } catch (Exception e) {
                MddLog.log(TAG, "No Old Database found");
            }
        }
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase(encryptionKey);

in the above code i have updated my DB version to 2 and i am trying to verify the version of the database by db.getVersion. The SQLiteDatabase.openOrCreateDatabase opens the version 2 DB fine, but it doesn't open the version 1 DB. Below is the logcat info
09-12 10:21:36.568: I/Database(31634): sqlite returned: error code = 26, msg = file is encrypted or is not a database
09-12 10:21:36.568: E/Database(31634): CREATE TABLE android_metadata failed
09-12 10:21:36.584: E/Database(31634): Failed to setLocale() when constructing, closing the database
09-12 10:21:36.584: E/Database(31634): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102)
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1968)
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:929)
09-12 10:21:36.584: E/Database(31634): at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:936)
09-12 10:21:36.584: E/Database(31634): at alc.rhapsody.mdd.database.DatabaseAdapter.openDatabase(DatabaseAdapter.java:239)
09-12 10:21:36.584: E/Database(31634): at alc.rhapsody.mdd.presenter.SplashViewPresenter.access$2(SplashViewPresenter.java:100)
09-12 10:21:36.584: E/Database(31634): at alc.rhapsody.mdd.presenter.SplashViewPresenter$AdmissionCtrl.doInBackground$5f8445a4(SplashViewPresenter.java:237)
09-12 10:21:36.584: E/Database(31634): at alc.rhapsody.mdd.presenter.SplashViewPresenter$AdmissionCtrl.doInBackground(SplashViewPresenter.java:1)
09-12 10:21:36.584: E/Database(31634): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-12 10:21:36.584: E/Database(31634): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-12 10:21:36.584: E/Database(31634): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-12 10:21:36.584: E/Database(31634): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-12 10:21:36.584: E/Database(31634): at java.lang.Thread.run(Thread.java:856)

It will be great if you could provide code snippet to update the old db.

@developernotes
Copy link
Member

Hi urspands,

SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(…) performs an upgrade to a temp file, upon completion it will replace the source file with the content of the temp file. It does not persist the user_version so you will have to set that following the upgrade. Reopen the database and call setVersion. This will be addressed in a future release of SQLCipher where we are introducing a PRAGMA to perform the migration, which will include user_version migration.

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