如何在 Android 中将 ORMLite 与 SQLCipher 一起使用?

发布于 2025-01-03 19:37:26 字数 360 浏览 1 评论 0原文

我想将 OrmLiteSQLCipher 在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

り繁华旳梦境 2025-01-10 19:37:26

我已将 Rejinderi 的答案提炼为 ORMLite 4.43 的补丁 并将其编译成 JAR 文件。要将其集成到您的 Android 项目中,请执行以下操作:

  1. 按照 SQLCipher for Android HOWTO 操作将 SQLCipher 添加到您的项目中
  2. 将 ORMLite 添加到您的项目中(请参阅 将 ORMLite 与 SQLite 结合使用的好教程是什么和 Android)
  3. libs/ormlite-android.jar 替换为 ormlite-android-sqlcipher.jar
  4. 将您的秘密数据库密码添加到适当的数据库打开方法

但是,您不应该相信我会提供未操作的 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:

  1. Follow the SQLCipher for Android HOWTO to get SQLCipher into your project
  2. Add ORMLite to your project (see What is a good tutorial for using ORMLite with SQLite and Android)
  3. Replace libs/ormlite-android.jar with ormlite-android-sqlcipher.jar
  4. Add your secret database password to the appropriate DB opening methods

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 the OrmLiteSqliteOpenHelper 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.

紅太極 2025-01-10 19:37:26

如何在 Android 中将 ORMLite 与 SQLCipher 一起使用?

应该有可能@Bruno。

一种可行的方法是复制 ORMLite 的 OrmLiteSqliteOpenHelper class 到您的项目中,将其重命名为 LocalOrmLiteSqliteOpenHelper 或其他名称,并将基类更改为 SQLCipher 帮助器类。我不敢相信他们没有将该类重命名为 SQLCipherSQLiteOpenHelper。 (抱怨)

public abstract class LocalOrmLiteSqliteOpenHelper
    extends info.guardianproject.database.sqlcipher.SQLiteOpenHelper {

另一种方法是让您的助手扩展 SQLCipher 的 SQLiteOpenHelper,然后自己从 OrmLiteSqliteOpenHelper 实现您需要的各种功能。然而,这需要更多的工作。在创建数据库时,ORMLite 必须对数据库连接进行一些操作,否则它会递归。

让我知道这些是否有效。

How can I use ORMLite with SQLCipher together in Android?

It should be possible @Bruno.

One way that should work is to just copy ORMLite's OrmLiteSqliteOpenHelper class into your project, rename it to LocalOrmLiteSqliteOpenHelper or something, and change the base class to be the SQLCipher helper class. I can't believe they didn't rename the class to be SQLCipherSQLiteOpenHelper. (grumble)

public abstract class LocalOrmLiteSqliteOpenHelper
    extends info.guardianproject.database.sqlcipher.SQLiteOpenHelper {

Another way would be to have your helper extend SQLCipher's SQLiteOpenHelper and then implement the various things you need from OrmLiteSqliteOpenHelper 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.

不必了 2025-01-10 19:37:26

为了更新那些需要帮助的人,您可以将 ormlite 的整个 android 源代码部分复制到您的项目中,并将 android db 包的所有引用的导入更改为 SQLCipher 的版本。

基本上,您必须将与 android.database.sqlite 匹配的所有包更改为 net.sqlcipher.database

例如,从

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

To

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

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

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

To

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

After that put the db password param to the call of getWriteableDatabase in AndroidConnectionSource.java

别再吹冷风 2025-01-10 19:37:26

应用 ge0rg 提供的补丁后,我发现 queryForAll() 抛出 NoSuchMethod 异常。经过一番调查,我发现这是 rawQuery 返回 net.sqlcipher.Cursor 的结果,而 getQuery (在 AndroidCompiledStatement 中)返回的光标是 android.database.Cursor 的结果。我只是将 AndoridCompiledStatement 的导入修改为:

import net.sqlcipher.Cursor;

并修改 getCursor() 以返回 android.database.Cursor:

public android.database.Cursor getCursor() { ...

通过这些更改,它似乎对我有用。不过我还需要多玩一会儿才能完全确定。

Applying the patch supplied by ge0rg I to found that queryForAll() throws a NoSuchMethod exception. After some investigation I discovered this is a result of rawQuery returning a net.sqlcipher.Cursor and the cursor returned by getQuery (in AndroidCompiledStatement) being an android.database.Cursor. I simply modified AndoridCompiledStatement's imports to:

import net.sqlcipher.Cursor;

And modified getCursor() to return a android.database.Cursor:

public android.database.Cursor getCursor() { ...

With these changes it appears to work for me. Though I'll need to play about a bit more to be completely sure.

冧九 2025-01-10 19:37:26

我知道这是很旧的线程。但最近我不得不走同样的路。我已经阅读了两个线程来寻找解决方案: this 这个

  1. 我按照 ge0rg 的 答案 进行操作,它几乎起作用了,但存在一些问题,我必须替换代码中的方法(我想避免它)。
  2. 我按照 Eliott Roynette 的建议此处进行操作,效果很好,只是我需要向 Helper 类添加一种方法来接受密码,并且我做到了。
  3. 我想将代码排除到 lib 中以获得更智能的解决方案而不是代码混合,所以我单独构建 lib/模块。

现在我有了有效的解决方案(您可以从 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.

  1. I followed ge0rg's answer, it almost worked, were some problems and I have to replace methods inside my code (I wanted to avoid it).
  2. I did what Eliott Roynette suggested here and it worked good except I needed to add one method to Helper class to accept password and I did it.
  3. I wanted to exclude code into lib to have more smart solution instead of code mix, so I build lib/module separately.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文