2.在一个目录下sqlitendk 建立jni目录,将三个文件展开到该目录下,并新建一个空白的
Android.mk文件,目录结构|
├─jni
│ Android.mk
│ sqlite3.c
│ sqlite3.h
│ sqlite3ext.h
│
3.修改Android.mk包含如下内容:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sqlite3802
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := sqlite3.c
include $(BUILD_SHARED_LIBRARY)
回到jni的上级目录,sqlitendk,运行%NDK_HOME%\ndk-build.cmd
编译开始,最后生成 d:\sqlitendk\libs\armeabi\libsqlite3802.so
├─libs
│ └─armeabi
│ libsqlite3802.so
在Android NDK c文件中包含 sqlite3.h 即可使用sqlite的全部功能了。
将编译好的libsqlite3802.so和sqlite 头文件 sqlite3.h复制到将要使用的
例子的jni目录下,并建立Android.mk文件,指明 libsqlite3802.so所在目录,
然后调用ndk-build或ndk-build.cmd编译即可生成db 运行文件,
需要将 db和libsqlite3802.so都是用adb push复制到手机上,并修改
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:libsqlite3802.so所在目录
然后修改db权限可执行,运行即可。
附件为一个读取android /data/data/目录下某个应用的数据库的例子。
dbtype.h
#ifndef DBTYPE_H_INCLUDED
#define DBTYPE_H_INCLUDED
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlite3.h"
#define DB_PATH "/data/data/home.sunose/databases/doubleball.db3"
#define MAX_REDBALL 33 //最大双色球红球
#define MAX_BLUEBALL 16 //最大双色球篮球球
#define REDBALL_NUMBER 6 //最多红球数目
#define STDCALL __stdcall
static char QUERY_DB[]="SELECT * FROM _DBHISTORY order by term asc";;
例子db.c
#include "dbtype.h"
#include <stdio.h>
char *zErrMsg;
int rc;
int db_open;
char **records;//所有记录
int nrow;
int ncol;
sqlite3 *db;
//pre declare
int query(char * sql);
int main()
{
char result[1024*1024];//1M
char buf[80];
char number[16];
printf("this is %d\n",MAX_REDBALL);
printf("this is %s\n",DB_PATH);
rc = sqlite3_open(DB_PATH, &db);
if(rc!=SQLITE_OK)
{
printf("%s ",zErrMsg);
}
query(QUERY_DB);
int i=0,j=0;
for(i=1;i<=nrow;i++)//line,注意,recoreds包含了列名字
{
strcpy(buf,"[");
for(j=0;j<ncol;j++)
{
if(j==0 || j==1) //term and date time
{
sprintf(number,"'%s'",(records[i*ncol+j]));
strcat(buf,number);
}
if(j>1) //term and date time
{
sprintf(number,"'%02s'",(records[i*ncol+j]));
strcat(buf,number);
}
if(j<(ncol-1))
{
strcat(buf,",");
}
}
strcat(buf,"],\n");
strcat (result,buf);
}
printf("%s",result);
}
int query(char * sql)
{
zErrMsg=NULL;
rc = sqlite3_get_table
(
db, /* An open database */
sql, /* SQL to be executed */
&records, /* Result written to a char *[] that this points to */
&nrow, /* Number of result rows written here */
&ncol, /* Number of result columns written here */
&zErrMsg /* Error msg written here */
);
if(zErrMsg!=NULL)
{
printf("%s",zErrMsg);
zErrMsg=NULL;
}
return rc;
}
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := db
LOCAL_SRC_FILES := db.c
LOCAL_LDLIBS := -LD:/test_Projects/testc/ -lsqlite3802
include $(BUILD_EXECUTABLE)
│ Android.mk
│ sqlite3.c
│ sqlite3.h
│ sqlite3ext.h
│
3.修改Android.mk包含如下内容:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sqlite3802
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := sqlite3.c
include $(BUILD_SHARED_LIBRARY)
回到jni的上级目录,sqlitendk,运行%NDK_HOME%\ndk-build.cmd
编译开始,最后生成 d:\sqlitendk\libs\armeabi\libsqlite3802.so
├─libs
│ └─armeabi
│ libsqlite3802.so
在Android NDK c文件中包含 sqlite3.h 即可使用sqlite的全部功能了。
将编译好的libsqlite3802.so和sqlite 头文件 sqlite3.h复制到将要使用的
例子的jni目录下,并建立Android.mk文件,指明 libsqlite3802.so所在目录,
然后调用ndk-build或ndk-build.cmd编译即可生成db 运行文件,
需要将 db和libsqlite3802.so都是用adb push复制到手机上,并修改
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:libsqlite3802.so所在目录
然后修改db权限可执行,运行即可。
附件为一个读取android /data/data/目录下某个应用的数据库的例子。
dbtype.h
#ifndef DBTYPE_H_INCLUDED
#define DBTYPE_H_INCLUDED
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlite3.h"
#define DB_PATH "/data/data/home.sunose/databases/doubleball.db3"
#define MAX_REDBALL 33 //最大双色球红球
#define MAX_BLUEBALL 16 //最大双色球篮球球
#define REDBALL_NUMBER 6 //最多红球数目
#define STDCALL __stdcall
static char QUERY_DB[]="SELECT * FROM _DBHISTORY order by term asc";;
例子db.c
#include "dbtype.h"
#include <stdio.h>
char *zErrMsg;
int rc;
int db_open;
char **records;//所有记录
int nrow;
int ncol;
sqlite3 *db;
//pre declare
int query(char * sql);
int main()
{
char result[1024*1024];//1M
char buf[80];
char number[16];
printf("this is %d\n",MAX_REDBALL);
printf("this is %s\n",DB_PATH);
rc = sqlite3_open(DB_PATH, &db);
if(rc!=SQLITE_OK)
{
printf("%s ",zErrMsg);
}
query(QUERY_DB);
int i=0,j=0;
for(i=1;i<=nrow;i++)//line,注意,recoreds包含了列名字
{
strcpy(buf,"[");
for(j=0;j<ncol;j++)
{
if(j==0 || j==1) //term and date time
{
sprintf(number,"'%s'",(records[i*ncol+j]));
strcat(buf,number);
}
if(j>1) //term and date time
{
sprintf(number,"'%02s'",(records[i*ncol+j]));
strcat(buf,number);
}
if(j<(ncol-1))
{
strcat(buf,",");
}
}
strcat(buf,"],\n");
strcat (result,buf);
}
printf("%s",result);
}
int query(char * sql)
{
zErrMsg=NULL;
rc = sqlite3_get_table
(
db, /* An open database */
sql, /* SQL to be executed */
&records, /* Result written to a char *[] that this points to */
&nrow, /* Number of result rows written here */
&ncol, /* Number of result columns written here */
&zErrMsg /* Error msg written here */
);
if(zErrMsg!=NULL)
{
printf("%s",zErrMsg);
zErrMsg=NULL;
}
return rc;
}
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := db
LOCAL_SRC_FILES := db.c
LOCAL_LDLIBS := -LD:/test_Projects/testc/ -lsqlite3802
include $(BUILD_EXECUTABLE)