IT人生(http://www.jb51.cc/article/p-ghleqsot-baw.html)
一次偶然的机会,用到了sqlite3开源数据库。当时要自己重新编译,获取源码过程有点纠结,现在在此做个记录,以备后用,也希望能帮助大家。
第一步:源码获取
在官网上选择 Source Code——>sqlite-amalgamation-3080002.zip(后面的数字会因版本更新有所不同)
如果官网下载慢,可以直接在Robin的CSDN下载获取。
第二步:目录构建:
这里假设home目录为工作目录,操作命令如下:
mkdir sqlite3
mv sqlite-amalgamation-3080002.zip sqlite3/
unzip sqlite-amalgamation-3080002.zip
解压后,我们会获得如下文件:
shell.c sqlite3.c sqlite3ext.h sqlite3.h
开始编译libsqlite3.so 库,
gcc sqlite3.c -shared -o libsqlite3.so -ldl -lpthread
开始编译sqlite3的shell命令,在命令行中会启动sqlite3的操作终端
gcc shell.c -L./ -lsqlite3 -o shell
到此为止,我们会得到如下几个文件:
libsqlite3.so Readme shell shell.c sqlite3.c sqlite3ext.h sqlite3.h
经过上面操作,sqlite3数据库构建成功了,在此分享一个数据库访问的case吧:
#include "sqlite3.h" #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include <string.h> int getCallCountCallback(void *para,int columnNum,char ** columnValue,char **columnName); bool getFuncCallCountByaddr(sqlite3 *sqlH,char* funcAddr,char** callCount); bool insertFuncCallCountByaddr(sqlite3 *sqlH,char * funcAddr,char* callCount); #define sqlSTRSIZE 1024 #define sqlITE_OK 0 void main(void){ char* funcAddr = "0x09882"; sqlite3 *sqlH = NULL; char* pathLib = "./testB.db"; sqlite3 *sqlHA = NULL; char* pathLibA = "./testA.db"; char* callCount = malloc(1024); if(sqlITE_OK == sqlite3_open(pathLib,&sqlH)) { getFuncCallCountByaddr(sqlH,funcAddr,&callCount); sqlite3_close(sqlH); }else{ printf("open failure!!!\n"); } printf("callCount = %s\n",callCount); if(sqlITE_OK == sqlite3_open(pathLibA,&sqlHA)) { insertFuncCallCountByaddr(sqlHA,callCount); sqlite3_close(sqlHA); }else{ printf("open failure!!!\n"); } free(callCount); } bool getFuncCallCountByaddr(sqlite3 *sqlH,char** callCount){ bool ret = false; char ** errmsg; char *outArgs = malloc(1024); char sqlStr[sqlSTRSIZE] = {0}; snprintf(sqlStr,sqlSTRSIZE,"SELECT callNum from ppa_infor where funcAddr='%s'",funcAddr); int rc=sqlite3_exec(sqlH,sqlStr,getCallCountCallback,outArgs,errmsg); if(rc != sqlITE_OK) { printf("%d\n",rc); } //callCount = outArgs; printf("outArgs = %s\n",outArgs); strcpy(*callCount,outArgs); ret = true; free(outArgs); return ret; } int getCallCountCallback(void *para,char **columnName){ printf("%s\t%s\n",columnName[0],columnValue[0]); strcpy((char*)(para),columnValue[0]); printf("para=%s\n",(char*)para); return 0; } bool insertFuncCallCountByaddr(sqlite3 *sqlH,char* callCount){ bool ret = false; char ** errmsg; char sqlStr[sqlSTRSIZE] = {0}; snprintf(sqlStr,"update perf_infor set callNum=%s where funcAddr='%s'",callCount,NULL,rc); } ret = true; return ret; }原文链接:https://www.f2er.com/sqlite/200883.html