SQLite的C語言介面

來源:互聯網
上載者:User

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

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.