sqlite c/c++ 編程接口
1. 安裝
在 c/c++ 程序中使用 sqlite 之前,我們需要確保機器上已經(jīng)有 sqlite 庫??梢圆榭?sqlite 安裝章節(jié)了解安裝過程。
2. c/c++ 接口 api
以下是重要的 c&c++ / sqlite 接口程序,可以滿足您在 c/c++ 程序中使用 sqlite 數(shù)據(jù)庫的需求。如果您需要了解更多細節(jié),請查看 sqlite 官方文檔。
序號 | api & 描述 |
---|---|
1 | sqlite3_open(const char *filename, sqlite3 **ppdb)
該例程打開一個指向 sqlite 數(shù)據(jù)庫文件的連接,返回一個用于其他 sqlite 程序的數(shù)據(jù)庫連接對象。 如果 filename 參數(shù)是 null 或 ':memory:',那么 sqlite3_open() 將會在 ram 中創(chuàng)建一個內(nèi)存數(shù)據(jù)庫,這只會在 session 的有效時間內(nèi)持續(xù)。 如果文件名 filename 不為 null,那么 sqlite3_open() 將使用這個參數(shù)值嘗試打開數(shù)據(jù)庫文件。如果該名稱的文件不存在,sqlite3_open() 將創(chuàng)建一個新的命名為該名稱的數(shù)據(jù)庫文件并打開。 |
2 | sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
該例程提供了一個執(zhí)行 sql 命令的快捷方式,sql 命令由 sql 參數(shù)提供,可以由多個 sql 命令組成。 在這里,第一個參數(shù) sqlite3 是打開的數(shù)據(jù)庫對象,sqlite_callback 是一個回調(diào),data 作為其第一個參數(shù),errmsg 將被返回用來獲取程序生成的任何錯誤。 sqlite3_exec() 程序解析并執(zhí)行由 sql 參數(shù)所給的每個命令,直到字符串結(jié)束或者遇到錯誤為止。 |
3 | sqlite3_close(sqlite3*)
該例程關閉之前調(diào)用 sqlite3_open() 打開的數(shù)據(jù)庫連接。所有與連接相關的語句都應在連接關閉之前完成。 如果還有查詢沒有完成,sqlite3_close() 將返回 sqlite_busy 禁止關閉的錯誤消息。 |
3. 連接數(shù)據(jù)庫
下面的 c 代碼段顯示了如何連接到一個現(xiàn)有的數(shù)據(jù)庫。如果數(shù)據(jù)庫不存在,那么它就會被創(chuàng)建,最后將返回一個數(shù)據(jù)庫對象。
#include <stdio.h> #include <sqlite3.h> int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } sqlite3_close(db); }
現(xiàn)在,讓我們來編譯和運行上面的程序,在當前目錄中創(chuàng)建我們的數(shù)據(jù)庫 test.db。您可以根據(jù)需要改變路徑。
$gcc test.c -l sqlite3 $./a.out opened database successfully
如果要使用 c++ 源代碼,可以按照下列所示編譯代碼:
$g++ test.c -l sqlite3
在這里,把我們的程序鏈接上 sqlite3 庫,以便向 c 程序提供必要的函數(shù)。這將在您的目錄下創(chuàng)建一個數(shù)據(jù)庫文件 test.db,您將得到如下結(jié)果:
-rwxr-xr-x. 1 root root 7383 may 8 02:06 a.out -rw-r--r--. 1 root root 323 may 8 02:05 test.c -rw-r--r--. 1 root root 0 may 8 02:06 test.db
4. 創(chuàng)建表
下面的 c 代碼段將用于在先前創(chuàng)建的數(shù)據(jù)庫中創(chuàng)建一個表:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *notused, int argc, char **argv, char **azcolname){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stdout, "opened database successfully\n"); } /* create sql statement */ sql = "create table company(" \ "id int primary key not null," \ "name text not null," \ "age int not null," \ "address char(50)," \ "salary real );"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, 0, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "table created successfully\n"); } sqlite3_close(db); return 0; }
上述程序編譯和執(zhí)行時,它會在 test.db 文件中創(chuàng)建 company 表,最終文件列表如下所示:
-rwxr-xr-x. 1 root root 9567 may 8 02:31 a.out -rw-r--r--. 1 root root 1207 may 8 02:31 test.c -rw-r--r--. 1 root root 3072 may 8 02:31 test.db
5. insert 操作
下面的 c 代碼段顯示了如何在上面創(chuàng)建的 company 表中創(chuàng)建記錄:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *notused, int argc, char **argv, char **azcolname){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create sql statement */ sql = "insert into company (id,name,age,address,salary) " \ "values (1, 'paul', 32, 'california', 20000.00 ); " \ "insert into company (id,name,age,address,salary) " \ "values (2, 'allen', 25, 'texas', 15000.00 ); " \ "insert into company (id,name,age,address,salary)" \ "values (3, 'teddy', 23, 'norway', 20000.00 );" \ "insert into company (id,name,age,address,salary)" \ "values (4, 'mark', 25, 'rich-mond ', 65000.00 );"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, 0, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "records created successfully\n"); } sqlite3_close(db); return 0; }
上述程序編譯和執(zhí)行時,它會在 company 表中創(chuàng)建給定記錄,并會顯示以下兩行:
opened database successfully records created successfully
6. select 操作
在我們開始講解獲取記錄的范例之前,讓我們先了解下回調(diào)函數(shù)的一些細節(jié),這將在我們的范例使用到。這個回調(diào)提供了一個從 select 語句獲得結(jié)果的方式。它聲明如下:
typedef int (*sqlite3_callback)( void*, /* data provided in the 4th argument of sqlite3_exec() */ int, /* the number of columns in row */ char**, /* an array of strings representing fields in the row */ char** /* an array of strings representing column names */ );
如果上面的回調(diào)在 sqlite_exec() 程序中作為第三個參數(shù),那么 sqlite 將為 sql 參數(shù)內(nèi)執(zhí)行的每個 select 語句中處理的每個記錄調(diào)用這個回調(diào)函數(shù)。
下面的 c 代碼段顯示了如何從前面創(chuàng)建的 company 表中獲取并顯示記錄:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create sql statement */ sql = "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序編譯和執(zhí)行時,它會產(chǎn)生以下結(jié)果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 20000.0 callback function called: id = 2 name = allen age = 25 address = texas salary = 15000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully
7. update 操作
下面的 c 代碼段顯示了如何使用 update 語句來更新任何記錄,然后從 company 表中獲取并顯示更新的記錄:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create merged sql statement */ sql = "update company set salary = 25000.00 where id=1; " \ "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序編譯和執(zhí)行時,它會產(chǎn)生以下結(jié)果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 25000.0 callback function called: id = 2 name = allen age = 25 address = texas salary = 15000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully
8. delete 操作
下面的 c 代碼段顯示了如何使用 delete 語句刪除任何記錄,然后從 company 表中獲取并顯示剩余的記錄:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create merged sql statement */ sql = "delete from company where id=2; " \ "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序編譯和執(zhí)行時,它會產(chǎn)生以下結(jié)果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 20000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully