设为首页 收藏本站
查看: 682|回复: 0

[经验分享] http://www.roman10.net/how-to-compile-sqlite-for-android-using-ndk/

[复制链接]

尚未签到

发表于 2016-12-1 07:35:46 | 显示全部楼层 |阅读模式
  原文链接:http://www.roman10.net/how-to-compile-sqlite-for-android-using-ndk/

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 codehere.
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, argv ? argv : "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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307834-1-1.html 上篇帖子: 处理Android SQLite 下篇帖子: (转)SQLite入门与分析(三)---内核概述(2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表