How to Compile SQLite for Android using NDK

http://www.roman10.net/how-to-compile-sqlite-for-android-using-ndk/
 Page has been shared successfully.

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,

#include <sqlite3.h>

 

static int callback(void *NotUsed, int argc, char **argv, char **azColName) {

    int i;

    for (i = 0; i < argc; ++i) {

        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

    }

    printf("\n");

    return 0;

}

int main(int argc, char **argv) {

    char create_table[100] = "CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY,name TEXT NOT NULL)";

    char insert_value[100] = "INSERT INTO customers VALUES('1', 'roman10')";

    sqlite3 *db;

    char *errMsg;

    int rv;

    if (argc != 2) {

        printf("Usage: %s database\n", argv[0]);

        return 1;

    }

    rv = sqlite3_open(argv[1], &db);

    if (rv) {

        printf("Cannot open database: %s\n", sqlite3_errmsg(db));

        sqlite3_close(db);

        return 1;

    }

    rv = sqlite3_exec(db, create_table, callback, 0, &errMsg);

    if (rv != SQLITE_OK) {

        printf("SQLite statement execution error: %s\n", errMsg);

    }

    rv = sqlite3_exec(db, insert_value, callback, 0, &errMsg);

    if (rv != SQLITE_OK) {

        printf("SQLite statement execution error: %s\n", errMsg);

    }

    sqlite3_close(db);

    return 0;

}

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,

#LOCAL_PATH is used to locate source files in the development tree.

#the macro my-dir provided by the build system, indicates the path of the current directory

LOCAL_PATH:=$(call my-dir)

 

#####################################################################

#            build sqlite3                                            #

#####################################################################

include $(CLEAR_VARS)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/sqlite-amalgamation-3070900

LOCAL_MODULE:=sqlite3

LOCAL_SRC_FILES:=sqlite-amalgamation-3070900/sqlite3.c

include $(BUILD_STATIC_LIBRARY)

#include $(BUILD_SHARED_LIBRARY)

 

 

#####################################################################

#            build our code                    #

#####################################################################

include $(CLEAR_VARS)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/sqlite-amalgamation-3070900

LOCAL_MODULE:=sqlitetest

LOCAL_SRC_FILES:=sqlite_test.c

LOCAL_STATIC_LIBRARIES:=libsqlite3

#LOCAL_SHARED_LIBRARIES:=libsqlite3

LOCAL_LDLIBS:=-llog -lm

#include $(BUILD_SHARED_LIBRARY)

include $(BUILD_EXECUTABLE)

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

References:
0. SQLite home page: http://www.sqlite.com/
1. An Introduction to SQLite C/C++ Interface: http://www.sqlite.org/cintro.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值