Android:Native C源码(NDK)如何使用sqlite3进行编程?

1. 首先要#include

如果遇到以下错误:

/home/andy/Desktop/andy/Android/alps/development/salemanagerd/salemanagerd.h:32:29: error: sqlite3_android.h: No such file or directory

则在Android.mk增加相应搜索路径,注意是相对路径,所以取决于你到源码所在目录:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= salemanagerd.c

LOCAL_MODULE:= salemanagerd

LOCAL_STATIC_LIBRARIES := libc

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug tests

LOCAL_C_INCLUDES += \
	$(LOCAL_PATH)/../../external/sqlite/dist \
	$(LOCAL_PATH)/../../external/sqlite/android 

LOCAL_SHARED_LIBRARIES := \
	libsqlite 
	
include $(BUILD_EXECUTABLE)
2. C代码参考:

#include 
 
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;
}

数据库参考路径:

#define DATABASEPATH "/data/data/com.xxxx.salemanager/databases/applist.sqlite"

3.编译源代码:

mmm development/salemanagerd

或参考当前使用到编译环境进行模块编译

4. 参考文档:

     1) http://www.sqlite.org/cintro.html

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

     3) http://jianlee.ylinux.org/Computer/Server/sqlite.html






你可能感兴趣的:(Android进阶)