1.参考下面这个下载编译及安装
http://blog.csdn.net/rill_zhen/article/details/7493712
2.测试例子:
http://blog.163.com/linux_world/blog/static/1408830732010215873622/
//包含一个头文件就可以使用所以sqlite的接口了
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
extern "C"
{
#include"./sqlite3.h"
};
void createdb();
void querydb();
int main()
{
createdb();
querydb();
return 0;
}
void createdb()
{
int ret;
sqlite3 *pdb = 0;
sqlite3_stmt *stmt = 0;
char *error = 0;
char *sql = "insert into table1 values('value11',:aaa)";
int index;
static void *value = "asdfadsfasdfjasdfjaksdfaskjdfakdsfaksfja";
ret = sqlite3_open("db1.sdb", &pdb); //打开数据库,跟打开文本文件一样
if( ret != SQLITE_OK )
return;
ret = sqlite3_exec(pdb, "create table table1(col1 char(20), col2 BLOB)", 0,0, &error );
if( ret != SQLITE_OK )
return;
ret = sqlite3_prepare(pdb, sql,strlen(sql), &stmt, &error);
if( ret != SQLITE_OK )
return;
index = sqlite3_bind_parameter_index(stmt, ":aaa");
ret = sqlite3_bind_blob(stmt, index, value, strlen(value), SQLITE_STATIC);
if( ret != SQLITE_OK )
return;
ret = sqlite3_step(stmt);
if( ret != SQLITE_DONE )
return;
sqlite3_close(pdb);
}
void querydb()
{
int ret;
sqlite3 *pdb = 0;
sqlite3_stmt *pstmt = 0;
char *error = 0;
char *sql = "select * from table1";
int len;
int i;
char *name;
void *value;
ret = sqlite3_open("db1.sdb", &pdb);
if( ret != SQLITE_OK )
return;
ret = sqlite3_prepare(pdb, sql, strlen(sql), &pstmt, &error);
if( ret != SQLITE_OK )
return;
while( 1 )
{
ret = sqlite3_step(pstmt);
if( ret != SQLITE_ROW )
break;
name = sqlite3_column_text(pstmt, 0);
value = sqlite3_column_blob(pstmt, 1);
len = sqlite3_column_bytes(pstmt,1 );
}
}
编译sample出错:
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlSym':
/opt/sqlite-autoconf-3070900/sqlite3.c:29926: undefined reference to `dlsym'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexLeave':
/opt/sqlite-autoconf-3070900/sqlite3.c:17807: undefined reference to `pthread_mutex_unlock'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexTry':
/opt/sqlite-autoconf-3070900/sqlite3.c:17769: undefined reference to `pthread_mutex_trylock'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexEnter':
/opt/sqlite-autoconf-3070900/sqlite3.c:17723: undefined reference to `pthread_mutex_lock'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexFree':
/opt/sqlite-autoconf-3070900/sqlite3.c:17680: undefined reference to `pthread_mutex_destroy'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexAlloc':
/opt/sqlite-autoconf-3070900/sqlite3.c:17654: undefined reference to `pthread_mutex_init'
/opt/sqlite-autoconf-3070900/sqlite3.c:17637: undefined reference to `pthread_mutexattr_init'
/opt/sqlite-autoconf-3070900/sqlite3.c:17638: undefined reference to `pthread_mutexattr_settype'
/opt/sqlite-autoconf-3070900/sqlite3.c:17639: undefined reference to `pthread_mutex_init'
/opt/sqlite-autoconf-3070900/sqlite3.c:17640: undefined reference to `pthread_mutexattr_destroy'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlClose':
/opt/sqlite-autoconf-3070900/sqlite3.c:29930: undefined reference to `dlclose'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlError':
/opt/sqlite-autoconf-3070900/sqlite3.c:29899: undefined reference to `dlerror'
/opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlOpen':
/opt/sqlite-autoconf-3070900/sqlite3.c:29885: undefined reference to `dlopen' collect2: ld returned 1 exit status
错误信息中提示有线程接口函数,需要线程的动态链接库,要加入-lpthread选项。在网上查找按照网友提供的资料dlopen(),dlclose(),dlerror(),dlsym()函数在头文件#include <dlfcn.h>中,同样需要添加编译选项-ldl。
# arm-linux-gcc -o test test.c -I /opt/build/include/ -L /opt/build/lib -static -lsqlite3 -lpthread -ldl