使用NDK开发SQLite3

尝试环境:

  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)目录下。

测试代码:

复制代码
 1 /* -- START -- */
 2 /*
 3  *代码借用:http://www.cnblogs.com/sardine/archive/2011/07/30/2121853.html
 4  */
 5 #include <sqlite3.h>
 6 #include <stdio.h>
 7 
 8 static int _sql_callback(void * notused, int argc, char ** argv, char ** szColName)
 9 {
10         int i;
11         for ( i=0; i < argc; i++ )
12         {
13                 printf( "%s = %s\n", *szColName, argv == 0 ? "NUL" : *argv );
14         }
15         return 0;
16 }
17 
18 
19 int main()
20 {
21     const char * sSQL1 = "create table users(userid varchar(20) PRIMARY KEY,age int,regtime datetime);";
22     const char * sSQL2 = "insert into users values('guilh',29,'2009-7-16');";
23     const char * sSQL3 = "select * from users;";
24 
25         sqlite3 * db = 0;
26         char * pErrMsg = 0;
27 
28         int ret = 0;
29         const char* const dbname = "gtest.db";
30     
31         //打开数据库,如果数据库不存在,会建立一个数据库
32     ret = sqlite3_open(dbname, &db);
33 
34     if ( ret != SQLITE_OK )
35     {
36         printf("open error! : %s", sqlite3_errmsg(db));
37         return(1);
38     }
39 
40     printf("open db OK!\n");
41 
42     // 执行建表SQL
43     sqlite3_exec( db, sSQL1, 0, 0, &pErrMsg );
44     if ( ret != SQLITE_OK )
45     {
46         //fprintf(stderr, "SQL error: %s\n", pErrMsg);
47         printf("SQL error: %s\n", pErrMsg);
48         sqlite3_free(pErrMsg);
49     }
50 
51     // 执行插入记录SQL
52     sqlite3_exec( db, sSQL2, 0, 0, &pErrMsg);
53 
54     // 查询数据表
55     sqlite3_exec( db, sSQL3, _sql_callback, 0, &pErrMsg);
56 
57     // 关闭数据库
58     sqlite3_close(db);
59     db = 0;
60 
61     return 0;
62 }
复制代码

Android.mk: Android的make文件

复制代码
 1 #### START ####
 2 LOCAL_PATH:= $(call my-dir)
 3 
 4 include $(CLEAR_VARS)
 5 #include $(LOCAL_PATH)/../config.mk
 6 
 7 LOCAL_SRC_FILES:= test.cpp
 8 
 9 
10 LOCAL_LDLIBS := -lcutils -licuuc -licui18n -lutils -licudata -lsqlite 
11 
12 
13 #LOCAL_PRELINK_MODULE := false
14 
15 LOCAL_MODULE :=  sqlite3
16 
17 include $(BUILD_EXECUTABLE)
18 
19 ### END ###
复制代码

如果此时直接编译的话会有如下提示:

复制代码
 1 $ make APP=sqlite3
 2 Android NDK: Building for application 'sqlite3'
 3 Executable     : sqlite3
 4 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)
 5 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)
 6 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)
 7 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_getShortDefinitionString_4_2'
 8 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_close_4_2'
 9 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf32_to_utf8'
10 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_open_4_2'
11 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `unorm_next_4_2'
12 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_isalpha_4_2'
13 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_strcollIter_4_2'
14 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_getSortKey_4_2'
15 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_length_from_utf32'
16 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_appendCharSafeBody_4_2'
17 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_strcoll_4_2'
18 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf8_length'
19 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `uiter_setUTF8_4_2'
20 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_toupper_4_2'
21 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `u_strtok_r_4_2'
22 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `utf32_at'
23 ./platforms/android-3/arch-arm/usr/lib/libsqlite.so: undefined reference to `ucol_setAttribute_4_2'
24 collect2: ld returned 1 exit status
25 build/core/build-binary.mk:324: recipe for target `out/apps/sqlite3/armeabi/sqlite3' failed
26 make: *** [out/apps/sqlite3/armeabi/sqlite3] Error 1
复制代码

按照上面提示,依次从目标环境(源码或是你手机中)拷贝libcutils.so libicuuc.so libicui18n.so libutils.so libicudata.so到NDK的lib目录(见上)下。

如果出现如下错误:

复制代码
 1 $ make APP=sqlite3
 2 Android NDK: Building for application 'sqlite3'
 3 Executable     : sqlite3
 4 ./platforms/android-3/arch-arm/usr/lib/libcutils.so: undefined reference to `ioprio_set'
 5 ./platforms/android-3/arch-arm/usr/lib/libicuuc.so: undefined reference to `mbstowcs'
 6 ./platforms/android-3/arch-arm/usr/lib/libcutils.so: undefined reference to `ioprio_get'
 7 ./platforms/android-3/arch-arm/usr/lib/libicuuc.so: undefined reference to `wcstombs'
 8 collect2: ld returned 1 exit status
 9 build/core/build-binary.mk:324: recipe for target `out/apps/sqlite3/armeabi/sqlite3' failed
10 make: *** [out/apps/sqlite3/armeabi/sqlite3] Error 1
复制代码

拷贝目标环境的libc.so替换掉你开发环境里面的。

执行结果如下:

1 # ./sqlite3
2 ./sqlite3
3 open db OK!
4 userid = guilh
5 userid = guilh
6 userid = guilh

生成的数据库如下:

整个测试项目源代码:SQLite3.rar

 

 数据库里面的regtime不对,是应该sql语句里面的时间不对。改下就ok:

1     const char * sSQL2 = "insert into users values('guilh',29,'2012-12-30 00:00:00');";

你可能感兴趣的:(使用NDK开发SQLite3)