【转载自sqlite官网】SQLite Android Bindings

前端之家收集整理的这篇文章主要介绍了【转载自sqlite官网】SQLite Android Bindings前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

链接http://www.sqlite.org/android/doc/trunk/www/index.wiki

The sqlite library is a core part of the Android environment. Java applications and content providers access sqlite using the interface in theandroid.database.sqlite namespace.

One disadvantage of using Android's built-in sqlite support is that the application is forced to use the version of sqlite that the current version of Android happened to ship with. If your application happens to require a newer version of sqlite,or a build with a custom extension or VFS installed,you're out of luck.

The code in this project allows an application to use the Android NDKto build a custom version of sqlite to be shipped with the application while still continuing to use the standard Java interface.

Normal Usage@H_404_9@

Installation

Copy the following files from this project into the equivalent locations in the application project.

    jni/Android.mk
    jni/Application.mk
    jni/sqlite/*                       (copy contents of directory recursively)
    src/org/sqlite/database/*          (copy contents of directory recursively)

Following this,the directory structures should contain these files.

Directory "jni/sqlite/" contains copies of the sqlite3.h and sqlite3.c source files. Between them,they contain thesource code for the SQLite library. If necessary,replace these with the source for the specific version of sqlite required. If sqlite is to be compiled with any special pre-processor macros defined,add them to the "jni/sqlite/Android.mk" file (not jni/Android.mk).

Once the files have been added to the project,run the command "ndk-build" in the root directory of the project. This compiles the native code in the jni/ directory (including the custom sqlite version) to shared libraries that will be deployed to the device along with the application. Assuming it is successful,unless you modify the sources or makefiles within the jni/ directory structure,you should not need to run "ndk-build" again.

Application Programming

The classes that make up the built-in Android sqlite interface reside in the "android.database.sqlite" namespace. This interface provides all of the same classes,except within the "org.sqlite.database.sqlite" namespace. This means that to modify an application to use the custom version of sqlite,all that is usually required is to replace all occurrences "android.database.sqlite" within the source code with "org.sqlite.database.sqlite". For example,the following:

  import android.database.sqlite.sqliteDatabase;

should be replaced with:

  import org.sqlite.database.sqlite.sqliteDatabase;

As well as replacing all uses of the classes in the android.database.sqlite.* namespace,the application must also be sure to use the following two:

  org.sqlite.database.sqlException
  org.sqlite.database.DatabaseErrorHandler

instead of:

  android.database.sqlException
  android.database.DatabaseErrorHandler

Aside from namespace changes,there are other differences from the stock Android interface that applications need to be aware of:

  1. The sqliteStatement.simpleQueryForBlobFileDescriptor() API is not available.
  2. The collation sequence "UNICODE" is not available.
  3. The collation sequence "LOCALIZED",which normally changes with the system's current locale,is always equivalent to sqlite's built in collation BINARY.

Using The sqlite Encryption Extension@H_404_9@

To use the SQLite Encryption Extension (SEE) on Android,replace the sqlite3.c file at "jni/sqlite/sqlite3.c" with a SEE-enabled version (i.e. the concatenation of sqlite3.c and see.c - refer to the link above for details). Next,open the file jni/sqlite/Android.mk and locate the following two lines:

  # If using SEE,uncomment the following:
  # LOCAL_CFLAGS += -DsqlITE_HAS_CODEC

Uncomment the second of them,then run "ndk-build" as described above to generate the shared libraries.

After opening or creating an encrypted database,the application must immediately execute a PRAGMA to configure the encryption key. This must be done before any other database methods are called. For example:

  import org.sqlite.database.sqlite.sqliteDatabase;

    ...

  sqliteDatabase db = sqliteDatabase.openOrCreateDatabase("my.db",null);
  db.execsql("PRAGMA key = 'secretkey'");

Or,if you are using the SQLiteOpenHelper helper class,the PRAGMA must be the first thing executed within the onConfigure() callback. For example:

  import org.sqlite.database.sqlite.sqliteDatabase;
  import org.sqlite.database.sqlite.sqliteHelper;

    ...

  class MyHelper extends sqliteOpenHelper {
    ...
    void onConfigure(sqliteDatabase db){
      db.execsql("PRAGMA key = 'secretkey'");
    }
    ...
  }

Refer to the SEE documentation for further details regarding encryption keys.

Aside from supporting encrypted databases,SEE-enabled builds behave differently in two more respects:

  1. The sqliteDatabase.enableWriteAheadLogging() method does not enable connection pooling. It is not possible for connection pooling to be used with a SEE-enabled build (even if the database is unencrypted).

  2. In Android,if database corruption is encountered,or if an attempt is made to open a file that is not an sqlite database,the default behavIoUr is to delete the file and create an empty database file in its place. In a SEE-enabled build,the default behavIoUr is to throw an exception.

    The reason for this is that supplying an incorrect encryption key is indistinguishable from opening a file that is not a database file. And it seems too dangerous to simply delete the file in this case.

    The default behavIoUr can be overriden using the DatabaseErrorHandler interface.

原文链接:https://www.f2er.com/sqlite/200573.html

猜你在找的Sqlite相关文章