您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

SQLite的C语言接口

2024/5/7 7:46:44发布15次查看
近我正在linux平台写一个软件,需要用到一个简单的数据库。mysql做数据库固然很好,但其数据是存放在服务器的。我想要的基本功能也
近我正在linux平台写一个软件,需要用到一个简单的数据库。mysql做数据库固然很好,但其数据是存放在服务器的。我想要的基本功能也就是使用c程序创建一个数据库本地文件,然后可以对这个数据库文件执行基本的sql操作. 就像在windows平台基于vc6.0的dao数据库编程一样(创建一个本地文件.mdb).
从网上找到了一个开源免费的数据库开发工具--sqlite, 网上的关于sqlite的介绍有很多,详细见官方网站: . 我发现sqlite正是我需要的. 总结一下几个特点:
1. 开放源代码
2. 程序特别小,在windows下应用程序sqlite.exe仅仅200kb以内。
3. 支持大多数sql指令,速度极快
4. 直接创建一个xxx.db, 就是一个数据库,不需要服务器支持
5. 简洁的c语言api接口
基于上面几点,足可以看出sqlite的强大和优异之处。源代码公开和程序特别小,甚至可以跨平台直接编译gcc -o sqlite3 *,将这融合到潜入式系统是多么的美妙!
在ubuntu6.10平台安装sqlite3及其开发包:
#sudo apt-get install sqlite3 libsqlite3-dev
链接这篇文章介绍了sqlite的使用方法:
下面是我总结的sqlite3与c接口的api
我用到的主要是下面几个函数(头文件sqlite3.h):
int sqlite3_open(const char*, sqlite3**); //打开一个数据库
int sqlite3_close(sqlite3*); //关闭
int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);//执行
int sqlite3_get_table(sqlite3*, const char *sql,char***result, int *nrow,int *ncolumn ,char **errmsg );
 //result中是以数组的形式存放所查询的数据,首先是表名,,再是数据;
 //nrow/ncolumn分别为查询语句返回的结果集的行数/列数,没有查到结果时返回0
sqlite3_errcode() 通常用来获取最近调用的api接口返回的错误代码.
sqlite3_errmsg() 则用来得到这些错误代码所对应的文字说明.
exec错误码
#define sqlite_ok           0   /* successful result */
#define sqlite_error        1   /* sql error or missing database */
#define sqlite_internal     2   /* an internal logic error in sqlite */
#define sqlite_perm         3   /* access permission denied */
#define sqlite_abort        4   /* callback routine requested an abort */
#define sqlite_busy         5   /* the database file is locked */
#define sqlite_locked       6   /* a table in the database is locked */
#define sqlite_nomem        7   /* a malloc() failed */
#define sqlite_readonly     8   /* attempt to write a readonly database */
#define sqlite_interrupt    9   /* operation terminated by sqlite_interrupt() */
#define sqlite_ioerr       10   /* some kind of disk i/o error occurred */
#define sqlite_corrupt     11   /* the database disk image is malformed */
#define sqlite_notfound    12   /* (internal only) table or record not found */
#define sqlite_full        13   /* insertion failed because database is full */
#define sqlite_cantopen    14   /* unable to open the database file */
#define sqlite_protocol    15   /* database lock protocol error */
#define sqlite_empty       16   /* (internal only) database table is empty */
#define sqlite_schema      17   /* the database schema changed */
#define sqlite_toobig      18   /* too much data for one row of a table */
#define sqlite_constraint  19   /* abort due to contraint violation */
#define sqlite_mismatch    20   /* data type mismatch */
#define sqlite_misuse      21   /* library used incorrectly */
#define sqlite_nolfs       22   /* uses os features not supported on host */
#define sqlite_auth        23   /* authorization denied */
#define sqlite_row         100  /* sqlite_step() has another row ready */
#define sqlite_done        101  /* sqlite_step() has finished executing */
应用实例:
定义
sqlite3 *db = null;
char * errmsg = null;
char sql_cmd[200];
打开文件:
int rc = sqlite3_open(xxx.db, &db);
if(rc) //打开失败
      printf(open database failed!\n);
  else
    printf(create the database successful!\n);
建立表格:
     sprintf(sql_cmd,create table datapro(package integer,offset \
       integer,lklen integer,base inteher,link integer,err integer););
     rc=sqlite3_exec(db,sql_cmd,0,0,&emsg); //建立表datapro
     if(rc==sqlite_ok) //建表成功
           printf(create the chn_to_eng table successful!\n);
      else
           printf(%s\n,emsg);
添加数据:
   sprintf(sql_cmd,insert into datapro values(%d,%d,%d,%d,%d,%d);,4,2345,268,9,3,3);
   rc=sqlite3_exec(pro_db,pro_sqlcmd,0,0,&emsg);
查询:
 int nrow=0, ncolumn=0;
 char **azresult; //存放结果
 sql=select * from datapro;
 sqlite3_get_table(db,sql,&azresult,&nrow,&ncolumn,&emsg);
//其中nrow为行数,ncolum为列数
 printf(\nthe result of querying is : \n);
 for(int i=1;i {
     for(int j=0;j         printf(%s    ,azresult[i*ncolumn+j]);
      printf(\n);
 }
删除:
 sprintf(sql_cmd,delete from datapro where package=1;) ;
 rc=sqlite3_exec(db,sql,0,0,&emsg);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录