I would like to use OrmLite with SQLCipher in my Android project, but both libraries have their own abstract SQLiteOpenHelper
class to implement. Java don't allow a class to extend two classes and if I implement separately, they will not communicate with each other.
How can I work with both together? How do I resolve the SQLiteOpenHelper
implementation problem?
Just to update those who need help on this you can copy the entire android source portion of ormlite into your project and change the import of all reference of android's db package to SQLCipher's version.
Basically you have to change all of the package which matches android.database.sqlite to net.sqlcipher.database
For example, from
To
After that put the db password param to the call of getWriteableDatabase in AndroidConnectionSource.java
It should be possible @Bruno.
One way that should work is to just copy ORMLite's
OrmLiteSqliteOpenHelper
class into your project, rename it toLocalOrmLiteSqliteOpenHelper
or something, and change the base class to be theSQLCipher
helper class. I can't believe they didn't rename the class to beSQLCipherSQLiteOpenHelper
. (grumble)Another way would be to have your helper extend
SQLCipher
's SQLiteOpenHelper and then implement the various things you need fromOrmLiteSqliteOpenHelper
yourself. That would take a bit more work however. ORMLite has to do a little dance with database connections while the database is being created otherwise it goes recursive.Let me know if either of these work.
Applying the patch supplied by ge0rg I to found that
queryForAll()
throws aNoSuchMethod exception
. After some investigation I discovered this is a result of rawQuery returning anet.sqlcipher.Cursor
and the cursor returned by getQuery (in AndroidCompiledStatement) being anandroid.database.Cursor
. I simply modified AndoridCompiledStatement's imports to:And modified getCursor() to return a android.database.Cursor:
With these changes it appears to work for me. Though I'll need to play about a bit more to be completely sure.
I know this is quite old thread. But I had to go the same way recently. I've read two threads in search for solution: this and this.
And now I have solution that works (you can clone working demo from GitHub). Placing my answer into both threads to help others in the future.
I have distilled the answer by Rejinderi into a patch for ORMLite 4.43 and compiled it into a JAR file. To integrate it in your Android project, do the following:
libs/ormlite-android.jar
withormlite-android-sqlcipher.jar
However, you should not trust me to provide an unmanipulated JAR file and follow the build instructions in the patch instead.
EDIT: With the patched library, the calls to
getReadableDatabase()
,getWritableDatabase()
and theOrmLiteSqliteOpenHelper
constructor need to be passed the password as additional parameter. If you are using a DB helper, extend it appropriately to pass the password to OrmLiteSqliteOpenHelper.