According to SQLite home page, SQLite is a open source software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
Self-contained means SQLite depends little on external libraries; serverless means SQLite doesn’t follow the client-server model that most other SQL database engines follow. SQLite reads and writes directly from the database files on disk. zero-configuraiton means SQLite is easy to deploy and set up; Transactional means all changes and queries are atomic, consistent, isolated and durable. Details of these terms can be found on SQLite home page at reference 0.
This post covers how to compile SQLite for using with native code. Currently Android only provides APIs to access SQLite database on SDK using Java. No NDK interface is publicly available. However, as SQLite is open source and self-contained, it’s possible to build an embedded version of SQLite for using with NDK.
0. Download SQLite Source Code
One can download the SQLite source code here. It’s easier to build the version with all C source code combined into a single file, so download the amalgamation version. Once downloaded, extract the source files to your Android project’s jni folder.
The source code consists of only four files, sqlite3.h, sqlite3.c, sqlite3ext.h, shell.c. If we want to build an embedded version of SQLite, we only need sqlite3.h and sqlite3.c. Shell.c is for building a command line utility to access SQLite database, sqlite3ext.h is for building extension for SQLite.
1. A Simple Example of Using SQLite
For simplicity, we provided a C program modified from SQLite website example, the code is as below,
The code accepts a database name as input parameter. It first uses sqlite3_open to open (or create, if the database doesn’t exist) a database. It then execute two SQL query. The first one create a new table “customers” with columns “id” and “name”. The second SQL query insert a record with id = 0, name = “roman10” into the table “customers”.
2. Android.mk to Build the Example
To build the example for Android, you can use the Android.mk file below,
Note that the above build file builds the sqlite as static library, you can also build it as shared library.
Also note that the example illustrates how to build executable using SQLite, you can also call sqlite API in your JNI method, and provide an interface for your Java code to access the SQLite APIs you have embedded.
3. Run the Code
The compiled sqlitetest program should be found under your Android project’s libs/armabi-* folder. You can use adb push to push the executable to your phone. And you might want to change the permission of the sqlitetest to executable. Note that this operation need rooted device.
But root is not required to build SQLite and provide database access through JNI. It’s the limitation of the example provided here. If you don’t have a rooted device, just study the code and build scripts and I believe you can still get the idea.
Below is a screenshot of running sqlitetest program we compiled under Android,
Figure 1. Execution of the Sqlitetest Program