SQLITE的C語言介面

2021-08-26 10:58:19 字數 4177 閱讀 5008

近我正在linux平台寫乙個軟體,需要用到乙個簡單的資料庫。mysql做資料庫固然很好,但其資料是存放在伺服器的。我想要的基本功能也就是使用c程式建立乙個資料庫本地檔案,然後可以對這個資料庫檔案執行基本的sql操作. 就像在windows平台基於vc6.0的dao資料庫程式設計一樣(建立乙個本地檔案.mdb).

從網上找到了乙個開源免費的資料庫開發工具--sqlite, 網上的關於sqlite的介紹有很多,詳細見官方**:

. 我發現sqlite正是我需要的. 總結一下幾個特點:

1. 開放源**

2. 程式特別小,在windows下應用程式sqlite.exe僅僅200kb以內。

3. 支援大多數sql指令,速度極快

4. 直接建立乙個***.db, 就是乙個資料庫,不需要伺服器支援

5. 簡潔的c語言api介面

基於上面幾點,足可以看出sqlite的強大和優異之處。源**公開和程式特別小,甚至可以跨平台直接編譯"gcc -o sqlite3 *",將這融合到潛入式系統是多麼的美妙!

在ubuntu6.10平台安裝sqlite3及其開發包:

#sudo apt-get install sqlite3 libsqlite3-dev

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("***.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

刪除:sprintf(sql_cmd,"delete from datapro where package=1;") ;

rc=sqlite3_exec(db,sql,0,0,&emsg);

C語言程式呼叫SQLite

寫個c語言程式呼叫sqlite 現在我們來寫個c c 程式,呼叫 sqlite 的 api 介面函式。下面是乙個c程式的例子,顯示怎麼使用 sqlite 的 c c 介面.資料庫的名字由第乙個引數取得且第二個引數或更多的引數是 sql 執行語句.這個函式呼叫sqlite3 open 在 22 行開啟...

C 程式的C語言介面

有時候我們得到乙個c 庫,但卻要在c語言中使用該庫中的函式,或者想編乙個在c語言下也容易使用的c 庫。首先我們寫兩個檔案,分別是foo.cpp和main.c,先看main.c void g void int main 只是簡單宣告了函式g 再在main 中呼叫。g 在foo.cpp中定義 inclu...

sqlite3 C語言程式設計

sqlite資料庫操作例程 plain view plain copy include include include int main memset sql,0 128 strcpy sql,create table student id integer,name varchar 10 f flo...