尝试环境:
Android2.2(源码) SDK
android-ndk-r7
尝试在NDK下面使用C语言做Android的SQLite3数据库存储功能。做了如下尝试:
在Android的源代码中找到sqlite3.h和libsqlite.so拷贝到NDK的lib(D:\android\android-ndk-r7-linux\platforms\android-3\arch-arm\usr\lib)和include(D:\android\android-ndk-r7-linux\platforms\android-3\arch-arm\usr\include)目录下。
测试代码:
/* -- START -- */ /* *代码借用:http://www.cnblogs.com/sardine/archive/2011/07/30/2121853.html */ #include <sqlite3.h> #include <stdio.h> static int _sql_callback(void * notused, int argc, char ** argv, char ** szColName) { int i; for ( i=0; i < argc; i++ ) { printf( "%s = %s\n", *szColName, argv == 0 ? "NUL" : *argv ); } return 0; } int main() { const char * sSQL1 = "create table users(userid varchar(20) PRIMARY KEY,age int,regtime datetime);"; const char * sSQL2 = "insert into users values('guilh',29,'2009-7-16');"; const char * sSQL3 = "select * from users;"; sqlite3 * db = 0; char * pErrMsg = 0; int ret = 0; const char* const dbname = "gtest.db"; //打开数据库,如果数据库不存在,会建立一个数据库 ret = sqlite3_open(dbname, &db); if ( ret != SQLITE_OK ) { printf("open error! : %s", sqlite3_errmsg(db)); return(1); } printf("open db OK!\n"); // 执行建表SQL sqlite3_exec( db, sSQL1, 0, 0, &pErrMsg ); if ( ret != SQLITE_OK ) { //fprintf(stderr, "SQL error: %s\n", pErrMsg); printf("SQL error: %s\n", pErrMsg); sqlite3_free(pErrMsg); } // 执行插入记录SQL sqlite3_exec( db, sSQL2, 0, 0, &pErrMsg); // 查询数据表 sqlite3_exec( db, sSQL3, _sql_callback, 0, &pErrMsg); // 关闭数据库 sqlite3_close(db); db = 0; return 0; }Android.mk: Android的make文件
#### START #### LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) #include $(LOCAL_PATH)/../config.mk LOCAL_SRC_FILES:= test.cpp LOCAL_LDLIBS := -lcutils -licuuc -licui18n -lutils -licudata -lsqlite #LOCAL_PRELINK_MODULE := false LOCAL_MODULE := sqlite3 include $(BUILD_EXECUTABLE) ### END ###如果此时直接编译的话会有如下提示:
$ make APP=sqlite3 Android NDK: Building for application 'sqlite3' Executable : sqlite3 D:/android/android-ndk-r7-linux/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/http://www.cnblogs.com/http://www.cnblogs.com/arm-linux-androideabi/bin/ld.exe: warning: libicuuc.so, needed by ./platforms/android-3/arch-arm/usr/lib/libsqlite.so, not found (try using -rpath or -rpath-link) D:/android/android-ndk-r7-linux/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/http://www.cnblogs.com/http://www.cnblogs.com/arm-linux-androideabi/bin/ld.exe: warning: libicui18n.so, needed by ./platforms/android-3/arch-arm/usr/lib/libsqlite.so, not found (try using -rpath or -rpath-link) D:/android/android-ndk-r7-linux/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/http://www.cnblogs.com/http://www.cnblogs.com/arm-linux-androideabi/bin/ld.exe: warning: libutils.so, needed by ./platforms/android-3/arch-arm/usr/lib/libsqlite.so, not found (try using -rpath or -rpath-link) ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_getShortDefinitionString_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_close_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf32_to_utf8' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_open_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `unorm_next_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_isalpha_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_strcollIter_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_getSortKey_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_length_from_utf32' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_appendCharSafeBody_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_strcoll_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_length' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `uiter_setUTF8_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_toupper_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_strtok_r_4_2' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf32_at' ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_setAttribute_4_2' collect2: ld returned 1 exit status build/core/build-binary.mk:324: recipe for target `out/apps/sqlite3/armeabi/sqlite3' failed make: *** [out/apps/sqlite3/armeabi/sqlite3] Error 1
按照上面提示,依次从目标环境(源码或是你手机中)拷贝libcutils.so libicuuc.so libicui18n.so libutils.so libicudata.so到NDK的lib目录(见上)下。
如果出现如下错误:
$ make APP=sqlite3 Android NDK: Building for application 'sqlite3' Executable : sqlite3 ./platforms/android-3/arch-arm/usr/lib/libcutils.so: undefined reference to `ioprio_set' ./platforms/android-3/arch-arm/usr/lib/libicuuc.so: undefined reference to `mbstowcs' ./platforms/android-3/arch-arm/usr/lib/libcutils.so: undefined reference to `ioprio_get' ./platforms/android-3/arch-arm/usr/lib/libicuuc.so: undefined reference to `wcstombs' collect2: ld returned 1 exit status build/core/build-binary.mk:324: recipe for target `out/apps/sqlite3/armeabi/sqlite3' failed make: *** [out/apps/sqlite3/armeabi/sqlite3] Error 1拷贝目标环境的libc.so替换掉你开发环境里面的。
###执行结果如下: # ./sqlite3 ./sqlite3 open db OK! userid = guilh userid = guilh userid = guilh生成的数据库如下:
整个测试项目源代码:SQLite3.rar
数据库里面的regtime不对,是应该sql语句里面的时间不对。改下就ok:
const char * sSQL2 = "insert into users values('guilh',29,'2012-12-30 00:00:00');";
函数在线列表:http://www.sqlite.org/c3ref/funclist.html
sqlite取表函数(推荐):
int sqlite3_get_table( sqlite3 *db, /* An open database */ const char *zSql, /* SQL to be evaluated */ char ***pazResult, /* Results of the query */ int *pnRow, /* Number of result rows written here */ int *pnColumn, /* Number of result columns written here */ char **pzErrmsg /* Error msg written here */ ); void sqlite3_free_table(char **result);API说明文件:http://www.sqlite.org/c3ref/free_table.html