分类: 数据库开发技术
2009-06-18 15:30:08
1. 下载sqlite3源代码
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. 写自己的数据库应用
#i nclude
#i nclude
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i
}
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
sqlite3_exec( db , sql , 0 , callback , &zErrMsg );
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES( 0 , 1 , 1 , '200605011206', 18.9 );" ;
sqlite3_exec( db , sql , 0 , callback , &zErrMsg );
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES(1 , 1 , 1 , '200605011306', 16.4 );" ;
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<( nrow + 1 ) * ncolumn ; 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下的文件查得。