xylz,imxylz

          關(guān)注后端架構(gòu)、中間件、分布式和并發(fā)編程

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks

          SQLite3 C語言API入門

          下載SQLite3

          我們下載sqlite源碼包,只需要其中的sqlite3.c、sqlite.h即可。

          最簡單的一個創(chuàng)建表操作

          #include <stdio.h>
          #include "sqlite3.h"

          int main(int argc,char *argv[]){
              const char *sql_create_table="create table t(id int primary key,msg varchar(128))";
              char *errmsg = 0;
              int ret = 0;

              sqlite3 *db = 0;
              ret = sqlite3_open("./sqlite3-demo.db",&db);
              if(ret != SQLITE_OK){
                  fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db));
                  return 1;
              }
              printf("Open database\n");

              ret = sqlite3_exec(db,sql_create_table,NULL,NULL,&errmsg);
              if(ret != SQLITE_OK){
                  fprintf(stderr,"create table fail: %s\n",errmsg);
              }
              sqlite3_free(errmsg);
              sqlite3_close(db);

              printf("Close database\n");

              return 0;
          }



          在這個操作中我們執(zhí)行了如下操作:

          • 打開數(shù)據(jù)庫
          • 執(zhí)行SQL語句
          • 關(guān)閉數(shù)據(jù)庫

          當(dāng)然這中間會有一些狀態(tài)的判斷以及內(nèi)存指針的釋放等。

          打開數(shù)據(jù)庫的API如下:

          int sqlite3_open(
            const char *filename,   /* Database filename (UTF-8) */
            sqlite3 **ppDb          /* OUT: SQLite db handle */
          );
          這里會引入一個非常復(fù)雜的sqlite3的數(shù)據(jù)結(jié)構(gòu)。這個根據(jù)需要以后酌情了解些。

           

          打開數(shù)據(jù)庫除了這種形式意外,還有sqlite3_open、sqlite3_open16、sqlite3_open_v2幾種形式,基本上類似。

          大部分sql操作都可以通過sqlite3_exec來完成,它的API形式如下:

          int sqlite3_exec(
            sqlite3*,                                  /* An open database */
            const char *sql,                           /* SQL to be evaluated */
            int (*callback)(void*,int,char**,char**),  /* Callback function */
            void *,                                    /* 1st argument to callback */
            char **errmsg                              /* Error msg written here */
          );

           

          各個參數(shù)的意義為:

          • sqlite3描述的是數(shù)據(jù)庫句柄
          • sql 要執(zhí)行的SQL語句
          • callback回調(diào)函數(shù)
          • void *回調(diào)函數(shù)的第一個參數(shù)
          • errmsg錯誤信息,如果沒有SQL問題則值為NULL

          回調(diào)函數(shù)式一個比較復(fù)雜的函數(shù)。它的原型是這樣的:

          int callback(void *params,int column_size,char **column_value,char **column_name){

           

          每一個參數(shù)意義如下:

          • params是sqlite3_exec傳入的第四個參數(shù)
          • column_size是結(jié)果字段的個數(shù)
          • column_value是返回記錄的一位字符數(shù)組指針
          • column_name是結(jié)果字段的名稱

          通常情況下callback在select操作中會使用到,尤其是處理每一行記錄數(shù)。返回的結(jié)果每一行記錄都會調(diào)用下“回調(diào)函數(shù)”。 如果回調(diào)函數(shù)返回了非0,那么sqlite3_exec將返回SQLITE_ABORT,并且之后的回調(diào)函數(shù)也不會執(zhí)行,同時未執(zhí)行的子查詢也不會繼續(xù)執(zhí)行。

          對于更新、刪除、插入等不需要回調(diào)函數(shù)的操作,sqlite3_exec的第三、第四個參數(shù)可以傳入0或者NULL。

          通常情況下sqlite3_exec返回SQLITE_OK=0的結(jié)果,非0結(jié)果可以通過errmsg來獲取對應(yīng)的錯誤描述。

          Windows下編譯:

          D:\home\dev\c>cl /nologo /TC sqlite3-demo.c sqlite3.c 

          GCC下編譯:

          $ gcc -o sqlite3-demo.bin sqlite3-demo.c sqlite3.c 

          刪除表操作

          為了防止垃圾數(shù)據(jù),我們在加載數(shù)據(jù)庫的時候刪除表操作。

          簡單的刪除操作可以直接使用sqlite3_exec即可。這里不需要回調(diào)函數(shù)以及回調(diào)函數(shù)的參數(shù)。 當(dāng)然需要可以關(guān)注sqlite3_exec返回的結(jié)果是否為SQLITE_OK的值。

              const char *sql_drop_table="drop table if exists t";
              const char *sql_create_table="create table t(id int primary key,msg varchar(128))";

              sqlite3_exec(db,sql_drop_table,0,0,&errmsg);
              sqlite3_exec(db,sql_create_table,0,0,&errmsg);

           

          插入數(shù)據(jù)

          插入第一條數(shù)據(jù)

              ret = sqlite3_exec(db,"insert into t(id,msg) values(1,'Ady Liu')",NULL,NULL,&errmsg);
              printf("Insert a record %s\n",ret == SQLITE_OK ? "OK":"FAIL");

           

          返回值ret為SQLITE_OK即操作成功。

          插入多條數(shù)據(jù),并刪除數(shù)據(jù)

              ret = sqlite3_exec(db,"insert into t(id,msg) values(1,'Ady Liu')",NULL,NULL,&errmsg);
              printf("Insert a record %s\n",ret == SQLITE_OK ? "OK":"FAIL");
              ret = sqlite3_exec(db,"insert into t(id,msg) values(2,'IMXYLZ')",NULL,NULL,&errmsg);
              printf("Insert a record %s\n",ret == SQLITE_OK ? "OK":"FAIL");
              ret = sqlite3_exec(db,"delete from t where id < 3",NULL,NULL,&errmsg);
              printf("Delete records: %s\n",ret == SQLITE_OK ? "OK":"FAIL");
          插入多條數(shù)據(jù),簡單的使用sqlite3_exec進行SQL執(zhí)行即可。當(dāng)然這里是完整的SQL字符串。

           

          預(yù)編譯操作

              int i = 0;
              sqlite3_stmt *stmt;
              char ca[255];

              //prepare statement
              sqlite3_prepare_v2(db,"insert into t(id,msg) values(?,?)",-1,&stmt,0);
              for(i=10;i<20;i++){
                  sprintf(ca,"HELLO#%i",i);
                  sqlite3_bind_int(stmt,1,i);
                  sqlite3_bind_text(stmt,2,ca,strlen(ca),NULL);
                  sqlite3_step(stmt);
                  sqlite3_reset(stmt);
              }
              sqlite3_finalize(stmt);
          預(yù)編譯操作比較麻煩的,完整的預(yù)編譯操作的流程是:
          1. 通過sqlite3_prepare_v2()創(chuàng)建一個sqlite3_stmt對象
          2. 通過sqlite3_bind_*()綁定預(yù)編譯字段的值
          3. 通過sqlite3_step()執(zhí)行SQL語句
          4. 通過sqlite3_reset()重置預(yù)編譯語句,重復(fù)操作2多次
          5. 通過sqlite3_finalize()銷毀資源

          sqlite3_prepare_v2()有個多種類似的形式,完整的API語法是:

          int sqlite3_prepare(
            sqlite3 *db,            /* Database handle */
            const char *zSql,       /* SQL statement, UTF-8 encoded */
            int nByte,              /* Maximum length of zSql in bytes. */
            sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
            const char **pzTail     /* OUT: Pointer to unused portion of zSql */
          );

           

          各個參數(shù)的定義為:

          • db為sqlite3的句柄
          • zSql為要執(zhí)行的SQL語句
          • nByte為要執(zhí)行語句在zSql中的最大長度,如果是負數(shù),那么就需要重新自動計算
          • ppStmt為預(yù)編譯后的句柄
          • pzTail預(yù)編譯后剩下的字符串(未預(yù)編譯成功或者多余的)的指針,通常沒什么用,傳入0或者NULL即可。


          綁定參數(shù)sqlite3_bind_*有多種形式,分別對應(yīng)不同的數(shù)據(jù)類型:

          int sqlite3_bind_blob(sqlite3_stmt*, intconst void*, int n, void(*)(void*));
          int sqlite3_bind_double(sqlite3_stmt*, intdouble);
          int sqlite3_bind_int(sqlite3_stmt*, intint);
          int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
          int sqlite3_bind_null(sqlite3_stmt*, int);
          int sqlite3_bind_text(sqlite3_stmt*, intconst char*, int n, void(*)(void*));
          int sqlite3_bind_text16(sqlite3_stmt*, intconst void*, intvoid(*)(void*));
          int sqlite3_bind_value(sqlite3_stmt*, intconst sqlite3_value*);
          int sqlite3_bind_zeroblob(sqlite3_stmt*, intint n);

           


          預(yù)編譯SQL語句中可以包含如下幾種形式:

          • ?
          • ?NNN
          • :VVV
          • @VVV
          • $VVV

          NNN代表數(shù)字,VVV代表字符串。

          如果是?或者?NNN,那么可以直接sqlite3_bind_*()進行操作,如果是字符串,還需要通過sqlite3_bind_parameter_index()獲取對應(yīng)的index,然后再調(diào)用sqlite3_bind_*()操作。這通常用于構(gòu)造不定條件的SQL語句(動態(tài)SQL語句)。

          查詢操作

          回調(diào)函數(shù)的解釋參考最上面的描述。 首先聲明一個回調(diào)函數(shù)。

          int print_record(void *,int,char **,char **); 


          查詢代碼

              //select data
              ret = sqlite3_exec(db,"select * from t",print_record,NULL,&errmsg);
              if(ret != SQLITE_OK){
                  fprintf(stderr,"query SQL error: %s\n",errmsg);
              }

           

          現(xiàn)在定義回調(diào)函數(shù),只是簡單的輸出字段值。

          int print_record(void *params,int n_column,char **column_value,char **column_name){
              int i;
              for(i=0;i<n_column;i++){
                  printf("\t%s",column_value[i]);
              }
              printf("\n");
              return 0;
          }

           

          不使用回調(diào)的查詢操作

          定義使用的變量

           char **dbresult; int j,nrow,ncolumn,index; 

          查詢操作

              //select table
              ret = sqlite3_get_table(db,"select * from t",&dbresult,&nrow,&ncolumn,&errmsg);
              if(ret == SQLITE_OK){
                  printf("query %i records.\n",nrow);
                  index=ncolumn;
                  for(i=0;i<nrow;i++){
                      printf("[%2i]",i);
                      for(j=0;j<ncolumn;j++){
                          printf(" %s",dbresult[index]);
                          index++;
                      }
                      printf("\n");
                  }
              }
              sqlite3_free_table(dbresult);

           

          sqlite3_get_table的API語法:

          int sqlite3_get_table(
            sqlite3 *db,          /* An open database */
            const char *zSql,     /* SQL to be evaluated */
            char ***pazResult,    /* Results of the query */
            int *pnRow,           /* Number of result rows written here */
            int *pnColumn,        /* Number of result columns written here */
            char **pzErrmsg       /* Error msg written here */
          );
          void sqlite3_free_table(char **result);

           

          其中:

          • db是sqlite3的句柄
          • zSql是要執(zhí)行的sql語句
          • pazResult是執(zhí)行查詢操作的返回結(jié)果集
          • pnRow是記錄的行數(shù)
          • pnColumn是記錄的字段個數(shù)
          • pzErrmsg是錯誤信息

          由于sqlite3_get_table是sqlite3_exec的包裝,因此返回的結(jié)果和sqlite3_exec類似。

          pazResult是一個(pnRow+1)*pnColumn結(jié)果集的字符串?dāng)?shù)組,其中前pnColumn個結(jié)果是字段的名稱,后pnRow行記錄是真實的字段值,如果某個字段為空,則對應(yīng)值為NULL。

          最后需要通過sqlite3_free_table()釋放完整的結(jié)果集。

          更新操作

           sqlite3_exec(db,"update t set msg='MESSAGE#10' where id=10",NULL,NULL,&errmsg); 

          當(dāng)然了,我們也可以使用預(yù)編譯方法進行更新操作。

          受影響的記錄數(shù)

          我們可以使用sqlite3_change(sqlite3 *)的API來統(tǒng)計上一次操作受影響的記錄數(shù)。

           ret = sqlite3_exec(db,"delete from t",NULL,NULL,&errmsg); 
          if(ret == SQLITE_OK){
          printf("delete records: %i\n",sqlite3_changes(db));
          }

          總結(jié)

          這里我們接觸了SQLITE3的13個API:

          • sqlite3_open()
          • sqlite3_exec()
          • sqlite3_close()
          • sqlite3_prepare_v2
          • sqlite3_bind_*()
          • sqlite3_bind_parameter_index()
          • sqlite3_step()
          • sqlite3_reset()
          • sqlite3_finalize()
          • sqlite3_get_table
          • sqlite3_change()
          • sqlite3_free()
          • sqlite3_free_table()

          事實上截止到SQLITE3.7.14(2012/09/03) 一共提供了204個API函數(shù)(http://www.sqlite.org/c3ref/funclist.html)。

          但最精簡的API函數(shù)大概有6個:

          • sqlite3_open()
          • sqlite3_prepare()
          • sqlite3_step()
          • sqlite3_column()
          • sqlite3_finalize()
          • sqlite3_close()

          核心API也就10個(在精簡API基礎(chǔ)上增加4個):

          • sqlite3_exec()
          • sqlite3_get_table()
          • sqlite3_reset()
          • sqlite3_bind()

          因此掌握起來還是比較容易的。

          完整的源碼地址: https://gist.github.com/3780669



          ©2009-2014 IMXYLZ |求賢若渴
          posted on 2012-09-25 16:34 imxylz 閱讀(28404) 評論(0)  編輯  收藏 所屬分類: 技術(shù)

          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 东港市| 延庆县| 江川县| 宿松县| 鄯善县| 元江| 密云县| 昆山市| 常宁市| 同仁县| 西青区| 皋兰县| 莱州市| 太仓市| 屏东市| 东明县| 准格尔旗| 桐乡市| 长阳| 灵璧县| 成武县| 德令哈市| 凤山县| 昌黎县| 新余市| 台东县| 隆德县| 平乡县| 霍州市| 勃利县| 岳阳市| 黄梅县| 安化县| 灵川县| 九龙坡区| 宜州市| 高密市| 保德县| 区。| 潮安县| 韶山市|