转载于:
http://blog.21ic.com/user1/765/archives/2008/49947.html张驿风
2008年8月19日
1. 下载sqlite3源代码
http://www.sqlite.org/sqlite-amalgamation-3.6.1.tar.gz
2. 解压源代码:
tar xvzf sqlite-amalgamation-3.6.1.tar.gz
3. 配置交叉编译到arm linux平台:
./configure --prefix=/home/rootfs/home/sqlite --target=arm-linux --host=arm-linux LD=arm-linux-ld
4. 编译:
make
5. 安装:
make install
头文件和生成的库文件将安装到:/home/rootfs/home/sqlite目录下。
6. 写自己的数据库应用
//error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );#i nclude
#i nclude
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=2 ){
fprintf(stderr, "Usage: %s DATABASE \n", argv[0]);
return 0;
}
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 0;
}
char *sql = " CREATE TABLE SensorData( \
ID INTEGER PRIMARY KEY, \
SensorID INTEGER, \
SiteNum INTEGER, \
Time VARCHAR(12), \
SensorParameter REAL \
);";
//使用sql字符串指定的sql语言 创建数据表SensorData
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES( 0 , 1 , 1 , '200605011206', 18.9 );" ;
//error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES(1 , 1 , 1 , '200605011306', 16.4 );" ;
//error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );
int nrow = 0, ncolumn = 0;
char **azResult;
int i;
//从数据表查询数据
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
printf( "row:%d column=%d" , nrow , ncolumn );
printf( "The result of querying is :" );
for( i=0 ; i printf( "azResult[%d] = %s \r\n", i , azResult[i] );
}
sqlite3_free_table( azResult );
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
7. 编写Makefile
prefix=/home/rootfs/home/sqlite
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Libs = -L${libdir} -lsqlite3 -lpthread
Cflags = -I${includedir}
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
radiodb: radiodb.o
$(CC) $^ $(Libs) -o $@
radiodb.o:radiodb.c
$(CC) $(Cflags) -c $^ -o $@
clean:
rm -rf radiodb *.o
in:
cp radiodb /home/rootfs/home/sqlite
注意makefile里的红色字体内容可以在sqlite安装目录的lib下的pkconfig下的文件查得。
而且sqlite3_get_table函数本身你可以看一下源代码,也是由
int sqlite3_prepare(sqlite3*, const char*, int, sqlite3_stmt**, const char**);
int sqlite3_bind_double(sqlite3_stmt*, int, double);
int sqlite3_bind_int(sqlite3_stmt*, int, int);
int sqlite3_bind_int64(sqlite3_stmt*, int, long long int);
int sqlite3_bind_null(sqlite3_stmt*, int);
int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
int sqlite3_step(sqlite3_stmt*);
这些API组合而成的.
所以正确的做法是自己使用上面API函数,这样当表中的数据很多的时候可以用__int64来接收ID