SQLite with Android NDK

26,362

Solution 1

It isn't possible to use the built-in SQLite via NDK (or it wasn't six months ago when I looked into this), that can only be accessed with Java. However it may be possible to link in your own completely separate C++ build of SQLite.

Solution 2

Just download the SQLite3 amalgamation source file from: http://www.sqlite.org/download.html

And then add sqlite3.c to your LOCAL_SRC_FILES variable in Android.mk.

Solution 3

See SQLite Android Bindings http://www.sqlite.org/android/doc/trunk/www/index.wiki which describes how to include sqlite3 for Android targets 15 (4.0.3) and greater. It's copied below.

SQLite Android Bindings

The SQLite library is a core part of the Android environment. Java applications and content providers access SQLite using the interface in the android.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 NDK to build a custom version of SQLite to be shipped with the application while still continuing to use the standard Java interface.

Normal Usage

Installation

Android API levels 15 (Android 4.0.3) and greater are supported. If targetting API level 16 or greater, use the default "trunk" branch of this project. Or, for API level 15, use the "api-level-15" branch. It is not possible to target an API level lower than 15.

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.

For API level 15 only, also copy the following:

src/org/sqlite/os/*         (copy contents of directory recursively) 

Directory "jni/sqlite/" contains copies of the sqlite3.h and sqlite3.c source files. Between them, they contain the source 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:

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

Solution 4

Disclaimer: i have only used this method for standalone executables, not libraries that implement JNI functions. It may work for a .so or not. Also, i'm working with a custom Android device not a phone.

You can use the built in SQLite via NDK but it's more of a hack than something supported. You need to nick sqlite3.h and libsqlite.so from an android source distribution and compile using them. Put sqlite3.h in your application source directory and you need to put the .so somewhere under the out/yourapp directory or build/platform/android-x/arch-arm/usr/lib for the linking step to finish. I have it in both places but i'm not sure which one is really needed.

You will end up linking to the libsqlite.so you provided but the binary will run fine using the system libsqlite.so on a target device.

Share:
26,362

Related videos on Youtube

Tommy
Author by

Tommy

Updated on July 09, 2022

Comments

  • Tommy
    Tommy almost 2 years

    Is it somehow possible to use SQLite with C++ on an Android phone? I haven't found any documentation around how this could be possible.

    • Krystian Bigaj
      Krystian Bigaj over 12 years
      code.google.com/p/sqlite-ndk - it includes small example how to use SQLite, but also sqlite3ndk gives you access to databases stored in 'assets' directory (2.3+). You can use of course SQLite directly from sqlite.org as there was no changes in sources.
    • shizhen
      shizhen about 5 years
  • Philippe Girolami
    Philippe Girolami about 13 years
    Not only "may" it work but it does. I simply added "sqlite.c" to my project : it contains the whole database
  • Vincent B.
    Vincent B. almost 13 years
    @bitbox: I am interested by this solution, could you give more details about how to deal with it ?
  • Tae-Sung Shin
    Tae-Sung Shin over 12 years
    Simple but accurate answer. Thanks!
  • idish
    idish over 11 years
    Hi gabor, I'm just trying to use sqlite3 library in my android app. I imported the sqlite3.c and sqlite3.h to my jni folder in my project. What should I do next?
  • Rajan1404930
    Rajan1404930 about 8 years
    Hi, Where i have to put sqlite3_create_function(); ,pls give any example.
  • fnc12
    fnc12 almost 7 years
    it says ` No rule to make target jni/../../../Classes/libs/sqlite/sqlite.c', needed by obj/local/armeabi/objs-debug/MyGame_shared/__/__/__/Classes/‌​libs/sqlite/sqlite.o‌​'. Stop.` I use CrystaX NDK with C++ code
  • Rich von Lehe
    Rich von Lehe almost 5 years
    @user457015 - Heh. Only the worst of C programmers would do that. The amalgamation file is a convenience for integration. It's certainly possible to use the original source files for building and integrating into your project, but it's not recommended.