使用sqlite3與C接口開發數據庫程序


最近我正在Linux平台寫一個軟件,需要用到一個簡單的數據庫。mysql做數據庫固然很好,但其數據是存放在服務器的。我想要的基本功能也就是使用C程序創建一個數據庫本地文件,然后可以對這個數據庫文件執行基本的sql操作. 就像在Windows平台基於VC6.0的DAO數據庫編程一樣(創建一個本地文件.mdb).

從網上找到了一個開源免費的數據庫開發工具--sqlite, 網上的關於sqlite的介紹有很多,詳細見官方網站: http://www.sqlite.com.cn/ .  我發現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的使用方法:
http://www.sqlite.com.cn/MySqlite/4/378.Html

下面是我總結的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<nrow+1;i++)
 {
     for(int j=0;j<ncolumn;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);

 

 

http://blog.chinaunix.net/uid-10747583-id-3262223.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM