如何开始使用安卓版的SQLCipher?[英] How to get started with SQLCipher for android?

本文是小编为大家收集整理的关于如何开始使用安卓版的SQLCipher?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要使用sqlcipher进行android ...我已经使用sqlite制作了我的应用程序,只想将其转换为sqlcipher.

问题是,我对sqlcipher一无所知.

我已经在此链接中读到了有关它的信息: android/

但我还不太清楚. 我想知道您是否可以为Android教程提供一些基本的SQLCIPHER,从绝对的基础知识中,所有内容都以一种简单的方式教授.

谢谢!

推荐答案

要正确使用SQL Cipher为Android,您必须使用外部库,并更改与DB相互作用的某些代码.

  1. 必须首先将这些添加到您的项目(在LIBS文件夹中.)请参阅此处以获取这些内容: http://sqlcipher.net/sqlcipher-for-android/

  2. 其次,您需要将icudt4dl.zip文件添加到您的资产文件夹中,此ZIP带有SQL Cipher库.

  3. 右键单击您的项目,转到属性,然后Java构建路径,然后包括Commons-Codec.jar,Guava-r09.jar,sqlcipher.jar等库.完成此操作后,进行清洁.

  4. 然后在您的应用程序中,而不是导入android.database.sqlite,您将导入导入net.sqlcipher.database

  5. 更改与DB交互的任何代码,示例:

    sqlitedatabase.loadlibs(context);

    字符串dbpath = this.getDatabasepath(" dbname.db").getPath();

    sqlitedatabase db = sqlitedatabase.openorcreateatabase(dbpath," dbpassword",null);

  6. 验证数据库是加密的,转到Eclipse中的DDMS透视图,单击"文件资源管理器"选项卡,导航到数据/数据/,单击.DB文件,然后选择"获取设备"文件,将其保存到桌面上并用文本编辑器打开它.查找您一直在数据库中插入的纯文本值,如果您仍然可以读取它们,则出现了问题.

在实施SQL Cipher之前,检查一些SQLITE教程可能也是一个好主意.这里提到了一个好的: android sqlite database-我从哪里开始,当时该教程已经开始进行记事本?

更新

现在,这个答案已过时,而Eclipse实际上被贬低了Android的开发.最近,我不得不在Android Studio上使用SQLCipher构建一个应用程序5 + 6,这是我遵循的步骤.

在Android Studio中,您可以将SQLCipher作为构建文件中的依赖项包含.更新您在构建Gradle中的依赖项以包括以下行:

dependencies{
    compile 'net.zetetic:android-database-sqlcipher:3.5.4@aar'
}

您可以在此处与版本保持最新: sqlcipher

除非我在LIB文件夹和资产文件夹中删除了SQLCipher文件,否则我的应用程序不会构建,但是之后它可以按预期工作.进行这些更改后,运行构建/清洁并检查是否有效.

上面提到的更改代码的相同步骤仍然存在.

其他推荐答案

虽然您仍然可以关注 Zetetic的Eclipse's eclipse tutorial 并添加.SO库您真的不需要在Android Studio中进行.只需添加一个gradle依赖性,例如compile net.zetetic:android-database-sqlcipher:3.3.1-2@aar,就可以了!

在这里您始终可以检查最新的AAR版本和在这里您可以阅读有关集成的更多信息.

其他推荐答案

//in build.gradle
implementation 'net.zetetic:android-database-sqlcipher:4.4.0@aar'
implementation "androidx.sqlite:sqlite:2.1.0"

 // in activity
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initialize sqlite libraries
        SQLiteDatabase.loadLibs(this)

        // initializing db
        val database = SQLiteDatabase.openOrCreateDatabase(
            "/some/path",
            "mypassword",
            null
        )

        // creating table if not exists
        database.execSQL("create table if not exists $DB_TABLE_NAME($DB_COLUMN1_NAME,  $DB_COLUMN2_SURNAME)")

        // get data from db
        s = loadFromDB()
    }




  private fun loadFromDb(): String {
        var s = ""
        var i = 0

        createTableIfNotExist()
        val cursor = database.rawQuery("select * from $DB_TABLE_NAME", null)
        cursor?.moveToFirst()
        if (!cursor.isAfterLast) {
            do {
                s += "col_${i++}: name:${cursor.getString(0)} surname:${cursor.getString(1)}\n"
            } while (cursor.moveToNext())

            cursor.close()
            return s
        }

        return s
    }

没有现实世界的示例和文档,入门有点困难! 这是一个简单的示例.使用非常方便!

如果您喜欢这个例子,请不要忘记出演并在需要的情况下做出贡献).谢谢!

本文地址:https://www.itbaoku.cn/post/597726.html

问题描述

I need to use SQLCipher for android...i've already made my app using SQLite and want to just convert it to SQLCipher.

The problem is, I know nothing about SQLCipher.

I have read about it in this link: http://sqlcipher.net/sqlcipher-for-android/

But i'm not too clear, still. I was wondering if you could provide some basic sqlcipher for android tutorials, where everything is taught in an easy way from the absolute basics.

Thanks!

推荐答案

To properly use SQL Cipher for Android you must use external libraries and change some of the code which interacts with your DB.

  1. These must first be added to your project (within the libs folder.) Refer here to get these: http://sqlcipher.net/sqlcipher-for-android/

  2. Secondly you need to add the icudt4dl.zip file to your assets folder, this Zip comes with the SQL Cipher libraries.

  3. Right click your project, go to properties then Java build path then include libraries such as commons-codec.jar, guava-r09.jar, sqlcipher.jar. Once this is done, do a build clean.

  4. Then within your App, instead of importing android.database.sqlite, you will import import net.sqlcipher.database

  5. Change any code which interacts with the DB, example:

    SQLiteDatabase.loadLibs(context);

    String dbPath = this.getDatabasePath("dbname.db").getPath();

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath,"dbPassword", null);

  6. Verify that the database is encrypted, go to DDMS perspective in Eclipse, click the file explorer tab, navigate to data/data/, click on the .db file and select get device file, save it to your desktop and open it with a text editor. Look for the plain text values you have been inserting into your database, if you can still read them, something has gone wrong.

It might also be a good idea to check out some SQLite tutorials before implementing SQL Cipher. A good one is mentioned here: Android sqlite database - where do i start as the tutorial has gone for notepad?

Update

This answer is outdated now and Eclipse is practically deprecated for Android Development. I recently had to build an app using SQLCipher on Android Studio for Android 5 + 6 and these are the steps I followed.

In Android Studio, you can include SQLCipher as a dependency in your build file. Update your dependencies in build gradle to include the following line:

dependencies{
    compile 'net.zetetic:android-database-sqlcipher:3.5.4@aar'
}

You can keep up to date with versions here: https://mvnrepository.com/artifact/net.zetetic/android-database-sqlcipher

My App wouldn't build unless I removed the SQLCipher files in the lib folder and the asset folder, but after that it worked as expected. Once you made these changes, run a build/clean and check if it works.

The same steps mentioned above with changing your code still stand.

其他推荐答案

While you can still follow Zetetic's Eclipse tutorial and add .so libraries yourself, you really don't need to do it in Android Studio. Just add a Gradle dependency, like compile net.zetetic:android-database-sqlcipher:3.3.1-2@aar and you're ready to go!

Here you can always check the latest aar version and here you can read more about integration.

其他推荐答案

//in build.gradle
implementation 'net.zetetic:android-database-sqlcipher:4.4.0@aar'
implementation "androidx.sqlite:sqlite:2.1.0"

 // in activity
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initialize sqlite libraries
        SQLiteDatabase.loadLibs(this)

        // initializing db
        val database = SQLiteDatabase.openOrCreateDatabase(
            "/some/path",
            "mypassword",
            null
        )

        // creating table if not exists
        database.execSQL("create table if not exists $DB_TABLE_NAME($DB_COLUMN1_NAME,  $DB_COLUMN2_SURNAME)")

        // get data from db
        s = loadFromDB()
    }




  private fun loadFromDb(): String {
        var s = ""
        var i = 0

        createTableIfNotExist()
        val cursor = database.rawQuery("select * from $DB_TABLE_NAME", null)
        cursor?.moveToFirst()
        if (!cursor.isAfterLast) {
            do {
                s += "col_${i++}: name:${cursor.getString(0)} surname:${cursor.getString(1)}\n"
            } while (cursor.moveToNext())

            cursor.close()
            return s
        }

        return s
    }

Getting started is a bit difficult without a real world example and documentation! Here is a simple example and documentation. It is very simple and convenient to use!

If you like the example, don't forget to star and contribute if you want). Thank you!