如何在 Android 中将 ORMLite 与 SQLCipher 一起使用?
我想将 OrmLite 与 SQLCipher 在我的 Android 项目中,但这两个库都有自己的抽象 SQLiteOpenHelper
类来实现。 Java不允许一个类扩展两个类,如果我单独实现,它们将不会相互通信。
我怎样才能与两者一起工作?如何解决 SQLiteOpenHelper
实现问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已将 Rejinderi 的答案提炼为 ORMLite 4.43 的补丁 并将其编译成 JAR 文件。要将其集成到您的 Android 项目中,请执行以下操作:
libs/ormlite-android.jar
替换为ormlite-android-sqlcipher.jar
但是,您不应该相信我会提供未操作的 JAR 文件并按照补丁中的构建说明进行操作。
编辑:使用修补后的库,需要调用
getReadableDatabase()
、getWritableDatabase()
和OrmLiteSqliteOpenHelper
构造函数将密码作为附加参数传递。如果您使用的是数据库助手,请适当扩展它以将密码传递给 OrmLiteSqliteOpenHelper。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.应该有可能@Bruno。
一种可行的方法是复制 ORMLite 的
OrmLiteSqliteOpenHelper
class 到您的项目中,将其重命名为LocalOrmLiteSqliteOpenHelper
或其他名称,并将基类更改为SQLCipher
帮助器类。我不敢相信他们没有将该类重命名为 SQLCipherSQLiteOpenHelper。 (抱怨)另一种方法是让您的助手扩展 SQLCipher 的 SQLiteOpenHelper,然后自己从 OrmLiteSqliteOpenHelper 实现您需要的各种功能。然而,这需要更多的工作。在创建数据库时,ORMLite 必须对数据库连接进行一些操作,否则它会递归。
让我知道这些是否有效。
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.
为了更新那些需要帮助的人,您可以将 ormlite 的整个 android 源代码部分复制到您的项目中,并将 android db 包的所有引用的导入更改为 SQLCipher 的版本。
基本上,您必须将与 android.database.sqlite 匹配的所有包更改为 net.sqlcipher.database
例如,从
To
After 将 db 密码参数放入AndroidConnectionSource.java 中 getWriteableDatabase 的调用
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
应用 ge0rg 提供的补丁后,我发现
queryForAll()
抛出NoSuchMethod 异常
。经过一番调查,我发现这是 rawQuery 返回 net.sqlcipher.Cursor 的结果,而 getQuery (在 AndroidCompiledStatement 中)返回的光标是 android.database.Cursor 的结果。我只是将 AndoridCompiledStatement 的导入修改为:并修改 getCursor() 以返回 android.database.Cursor:
通过这些更改,它似乎对我有用。不过我还需要多玩一会儿才能完全确定。
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.
我知道这是很旧的线程。但最近我不得不走同样的路。我已经阅读了两个线程来寻找解决方案: this 和这个。
现在我有了有效的解决方案(您可以从 GitHub 克隆工作演示) 。将我的答案放入两个线程中以帮助将来的其他人。
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.