老房原创修改版
-------------------------------------------------------------------------分隔-------------------------------------------------------------------------------------------------------
sqlite是一款轻型的数据库,它的设计目标是嵌入式的,目前已经在很多嵌入式产品中使用了它,占用资源非常的低,在嵌入式设备中可能只需要几百K的内存就够了;
以下采用arm-linux-gcc4.2.2 eabi,经老房测试,在飞凌6410开发板上运行通过:
1、下载sqlite3源代码、解压
http://www.sqlite.org/sqlite-src-3070900.zip
下载sqlite-src-3070900.zip解压
2、配置交叉编译到arm平台:
./configure --prefix=/usr/local/sqlite --host=arm-linux CC=arm-linux-gcc
3、编译:
make
4、安装:
make install 头文件和生成的库文件将安装到PC的/usr/local/sqlite目录;
将sqlite目录拷贝到开发板文件系统的/usr/local;
将/usr/local/sqlite/lib添加到LD_LIBRARY_PATH(在开发板文件系统/etc/profile中修改);
将/usr/local/sqlite/bin添加到PATH(在开发板文件系统/etc/profile中修改);
5、写自己的数据库程序,保存为radiodb.c(这个程序我不是原创)
#include <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed,int argc,char **argv,char **azColName){
int i;
for(i=0; i<argc; 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
sqlite3_exec( db,sql,callback,&zErrMsg );
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES( 0,1,'200605011206',18.9 );" ;
sqlite3_exec( db,&zErrMsg );
//插入数据到数据表
sql = "INSERT INTO 'SensorData' VALUES(1,'200605011306',16.4 );" ;
sqlite3_exec( db,&zErrMsg );
int nrow = 0,ncolumn = 0;
char **azResult;
int i;
//从数据表查询数据
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db,&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;
}
6、编写Makefile
prefix=/usr/local/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
7、编译,得到radiodb,拷贝到开发板文件系统/usr/local,NFS挂载根文件系统后,超级终端执行:
cd /usr/local
./radiodb testdb