`
445822357
  • 浏览: 744118 次
文章分类
社区版块
存档分类
最新评论

http://www.roman10.net/how-to-compile-sqlite-for-android-using-ndk/

阅读更多

原文链接: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[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


分享到:
评论

相关推荐

    android-async-http 源码

    Asynchronous Http Client ... compile 'com.loopj.android:android-async-http:1.5.0-SNAPSHOT' } Documentation, Features and Examples Full details and documentation can be found on the project page here: ...

    01.内涵段子.zip

    内涵段子脑筋急转弯抓取 网址:http://www.neihan8.com 步骤: ... p = re.compile(r'&lt;div class="text-.*?title="&gt;(.*?)&lt;/div&gt;',re.S) 3. 代码 1. 发请求 2. 用正则匹配 3. 保存爬取内容

    ffmpeg-2.5.2-win64-dev.7z

    This is a FFmpeg Win64 shared build by Kyle ...This build was compiled using the MinGW-w64 toolchain: &lt;http://mingw-w64.sourceforge.net/&gt; Licenses for each library can be found in the 'licenses' folder.

    ffmpeg-2.5.2-win64-shared.7z

    This is a FFmpeg Win64 shared build by Kyle ...This build was compiled using the MinGW-w64 toolchain: &lt;http://mingw-w64.sourceforge.net/&gt; Licenses for each library can be found in the 'licenses' folder.

    FFmpeg win64

    This build was compiled using the MinGW-w64 toolchain: &lt;http://mingw-w64.sourceforge.net/&gt; Licenses for each library can be found in the 'licenses' folder. DownLoad By http://www.veryhuo.com

    Android代码-Croller

    [Platform](https://img.shields.io/badge/platform-Android-yellow.svg)](https://www.android.com) [![API](https://img.shields.io/badge/API-16+-brightgreen.svg?style=flat)]...

    Android代码-rx-java-extensions

    This library allow simple implementation for some tasks in android Usage Add library to project dependencies. repositories { maven { url "https://jitpack.io" } } dependencies { // snapshot ...

    Android代码-ArcLayout

    ArcLayout With Arc Layout explore new styles and approaches on ...The new Star Wars movie is coming soon, so I decided to design this cinema app screen with one of the Rogue One posters. Also, I tri

    android增量更新

    http://blog.csdn.net/hmg25/article/details/8100896 jni提供的接口: public native String bsdiff(String oldFilePath, String newFilePath, String patchFilePath); public native String bspatch(String ...

    Android代码-AppUpdate

    android app update library Screenshots Usage setup Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories: allprojects { repositories ...

    开源onlyoffice7.1社区版重新编译去除链接数量20限制下载

    自行编译教程https://blog.csdn.net/u011400204/article/details/124781470 安装参考https://helpcenter.onlyoffice.com/installation/docs-community-compile.aspx SpellChecker 模版在最新版中已经去除无需启动...

    spring 最新框架jar

    ### Compile and test; build all jars, distribution zips, and docs `./gradlew build` ... and discover more commands with `./gradlew tasks`. See also the [Gradle build and release FAQ][]. ## ...

    libxml2-2.9.1.tar.gz

    should compile as part of the build or as any application would. Launch them from this directory to get results, runtest checks the proper functionning of libxml2 main APIs while testapi does a ...

    UE(官方下载)

    Create a custom user tool to compile Java code, using the command line, from within UltraEdit Configure UltraEdit with javascript lint How to check your JavaScript source code for common mistakes ...

    Android代码-WsManager

    WsManager A library that simplifies the use of OkHttp ...How to use Instantiate a WsManager object: OkHttpClient okHttpClient = new OkHttpClient().newBuilder() .pingInterval(15, TimeUnit.SECONDS)

    Android代码-ProductLayer-SDK-for-Android

    ProductLayer SDK for Android The ultimate product information API, enabling a new breed of product-centric apps. This project contains the Android SDK. We also provide SDKs for iOS and Java. See ...

    Android代码-PicCrop

    在ucrop 2.2.0(compile 'com.yalantis:ucrop:2.2.0')基础上封装, 如果以后ucrop以后升级,那么本工具类只需要改CropConfig里字段就可以.无需更改api. 甚至,切换其他图片裁剪框架,也是改方法内部实现即可,不用更换各处...

    Android代码-Android rtmp rtsp 推流客户端

    rtmp-rtsp-stream-client-java ...Library for stream in RTMP and RTSP. All code in Java. If you need a player see this project: https://github.com/pedroSG94/vlc-example-streamplayer Wiki ...

    LINUX gd-2.0.35.tar.gz

    While gd will compile and install even without these, we suggest that at least zlib and libpng are installed, and recommend that freetype and jpeg are installed as well: 1. zlib, available from ...

    htcp(给C++代码添加html标记)

    执行make命令,编译成功之后执行 ./compile &gt; res , 将转换后的结果重定向到res文件 , Compile.cc就是主函数所在的文件. Makefile http://jinyun2012.blog.sohu.com/158883840.html type.hpp ...

Global site tag (gtag.js) - Google Analytics