Heim  >  Artikel  >  Datenbank  >  SQLite的C语言接口

SQLite的C语言接口

WBOY
WBOYOriginal
2016-06-07 17:12:20931Durchsuche

近我正在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);

linux

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Oracle interval 数据类型Nächster Artikel:DataGuard角色切换