Access to WeChat WCDB for Android

Access to WCDB

Android project access to WCDB, you can choose to access through Maven or through AAR package.

Access via Maven

For most developers, it is recommended to use Maven to access WCDB, just add the WCDB dependency under build.gradle of the APP module

dependencies {
    // Modify "1.0.0" to the actual referenced version
    compile 'com.tencent.wcdb:wcdb-android:1.0.0'
}


- Access via AAR package

You can also download the AAR package from the release page and import it into the project to access WCDB.

In Android Studio select File -> New -> New Module... -> Import JAR/AAR Package
Add WCDB to the dependencies in File -> Project Structure... or edit the build.gradle of the APP module:
dependencies {
    compile project(':wcdb')
}


- Select the connected CPU architecture

WCDB includes dynamic libraries of four architectures: armeabi, armeabi-v7a, arm64-v8a, x86. If your application wants to access only one or several architectures, you can add the following code to the build.gradle of the APP module to select Architecture to access:

    android {
        defaultConfig {
            ndk {
                // Only access armeabi-v7a and x86 architectures
                abiFilters 'armeabi-v7a', 'x86'
            }
        }
    }


Migrating to WCDB

  • WCDB android uses almost the same interface as the Android SDK SQLite framework
  • If your APP used the database interface of Android SDK before, just change android.database.* in import to com.tencent.wcdb., and android.database.sqlite. to com.tencent.wcdb.database.* That's it.
  • If you used SQLCipher Android Binding before, you also need to modify the import accordingly.

The only difference between encrypted and non-encrypted databases is when they are opened, and the subsequent operations after opening are consistent with the Android SDK. For details, please refer to the WCDB API documentation.

Migrating from a non-encrypted database to an encrypted database

If you used a non-encrypted database before, and want to migrate to an encrypted database and keep the original data after access, you need to use the SQL function sqlcipher_export() to migrate.

For details, please see the sample-encryptdb sample, which demonstrates how to use SQLiteOpenHelper to implement data migration from non-encrypted to encrypted data and schema upgrade.

Note: WCDB has extended the sqlcipher_export() function. Originally, it only accepts one parameter for which ATTACHED DB to export to. Now it can accept a second parameter to specify which DB to export from. So the import can be implemented in reverse:
ATTACH 'old_database' AS old;
 SELECT sqlcipher_export('main', 'old'); -- import from 'old' to 'main'
    DETACH old;


Migrating from SQLCipher Android

If you are using SQLCipher database before and want to migrate to WCDB library and keep the original database file, you need to make a little change in the code.
    String passphrase = "passphrase";

    SQLiteCipherSpec cipher = new SQLiteCipherSpec() // encryption description object
        .setPageSize(1024) // SQLCipher default Page size is 1024
        .setSQLCipherVersion(3); // 1,2,3 correspond to the SQLCipher database created by 1.x, 2.x, 3.x respectively
        // If other PRAGMAs have been used before, additional options can be added

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
        "path/to/database",     // DB 路径
        passphrase.getBytes(), // WCDB password parameter type is byte[]
        cipher, // the encrypted description object created above
        null,                   // CursorFactory
        null                    // DatabaseErrorHandler
        // The SQLiteDatabaseHook parameter is removed, and specifying the parameter in cipher can achieve the same purpose
    );


  • The key changes are converting the password to byte[] and passing in SQLiteCipherSpec to describe the encryption method. The encryption method must be the same as the previous SQLCipher setting, otherwise an error will be reported. It is recommended to test first before going online.
  • SQLCipher password and encryption method errors may cause the SQLite framework to think that it is damaged and trigger the DatabaseErrorHandler. The default implementation will rename or delete the damaged DB. If this behavior is not what you want, be sure to customize the DatabaseErrorHandler.


*Note* If SQLCipher's SQLiteDatabase.loadLibs(…) is called before, you can delete it and WCDB will automatically load the dynamic library when it is first referenced.

quote

Access and Migration

https://github.com/Tencent/wcdb/wiki/Android


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326160393&siteId=291194637