嵌入式数据库主要是Berkeley DB和sqlite,BDB功能很强可以应用于工业,但不是关系数据库,sqlite免费小巧且速度快。
1、下载源代码并编译安装
在http://www.sqlite.org/download.html下载sqlite-autoconf-3080100.tar.gz,解压缩后./configure ; make ; make install,则生成了sqlite3程序和500多k的libsqlite3.so.0.8.6库文件
其中源代码包括sqlite3.c和sqlite3.h,sqlite3.h在程序中需要用到
2、程序部署
将库文件libsqlite3.so.0.8.6拷贝到部署机即可
3、测试程序
#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; int rc; char *zErrMsg = 0; //1.create or open database file rc = sqlite3_open("my.db",&db); if( rc ){ fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } //2.create table rc = sqlite3_exec(db,"create table phonenumber(id int,name varchar(30));",NULL,&zErrMsg); if( rc!=sqlITE_OK ){ fprintf(stderr,"sql error: %s\n",zErrMsg); sqlite3_free(zErrMsg); } //3.insert data rc = sqlite3_exec(db,"insert into phonenumber values(1,'abc')",zErrMsg); sqlite3_free(zErrMsg); } //4.select and show data sqlite3_exec(db,"select * from phonenumber",callback,zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); return 0; }