SQLite - C/C++
安裝
在 C/C++ 程式中使用 SQLite 之前,我們需要確保機器上已經有 SQLite 函式庫。可以查看 SQLite 安裝章節以了解安裝過程。
C/C++ 介面 API
以下是重要的 C&C++ / SQLite 介面程序,可滿足您在 C/C++ 程式中使用 SQLite 資料庫的需求。如果您需要了解更多細節,請查看 SQLite 官方文件。
序號 | API & 描述 |
---|---|
#1 | sqlite3_open(const char *filename, sqlite3 **ppDb) 該例程開啟一個指向SQLite 資料庫檔案的連接,傳回一個用於其他SQLite 程式的資料庫連接物件。 如果 filename 參數是 NULL 或 ':memory:',那麼 sqlite3_open() 將會在 RAM 中建立一個記憶體資料庫,這只會在 session 的有效時間內持續。 如果檔案名稱 filename 不為 NULL,那麼 sqlite3_open() 將使用這個參數值嘗試開啟資料庫檔案。如果該名稱的檔案不存在,sqlite3_open() 將建立一個新的命名為該名稱的資料庫檔案並開啟。 |
2 | sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg) 這個例程提供了執行SQL 指令的捷徑,SQL 指令由sql 參數提供,可以由多個SQL 指令組成。 在這裡,第一個參數sqlite3 是開啟的資料庫對象,sqlite_callback 是一個回調,data 作為其第一個參數,errmsg 將被傳回用來取得程式產生的任何錯誤。 sqlite3_exec() 程式解析並執行由 sql 參數所給的每個指令,直到字串結束或遇到錯誤為止。 |
3 | sqlite3_close(sqlite3*) 該例程關閉之前呼叫sqlite3_open() 開啟的資料庫連線。所有與連接相關的語句都應在連接關閉之前完成。 如果還有查詢沒有完成,sqlite3_close() 會傳回 SQLITE_BUSY 禁止關閉的錯誤訊息。 |
連接資料庫
下面的 C 程式碼片段顯示如何連接到一個現有的資料庫。如果資料庫不存在,那麼它就會被創建,最後將傳回一個資料庫物件。
#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); }
現在,讓我們來編譯和運行上面的程序,在當前目錄中創建我們的資料庫 test.db。您可以根據需要改變路徑。
$gcc test.c -l sqlite3 $./a.out Opened database successfully
如果要使用 C++ 原始程式碼,可以按照下列所示編譯程式碼:
$g++ test.c -l sqlite3
在這裡,把我們的程式連結上 sqlite3 函式庫,以便向 C 程式提供必要的函式。這將在您的目錄下建立一個資料庫檔案test.db,您將得到以下結果:
-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
建立表格
下面的C 程式碼片段將用於在先前建立的資料庫中建立一個表格:
#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; }
上述程式編譯和執行時,它會在test.db 檔案中建立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
INSERT 操作
#下面的C 程式碼片段顯示如何在上面建立的COMPANY 表中建立記錄:
#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; }
上述程式編譯和執行時,它會在COMPANY 表中建立給定記錄,並會顯示以下兩行:
Opened database successfully Records created successfully
SELECT 操作
在我們開始講解取得記錄的實例之前,讓我們先了解下回呼函數的一些細節,這將在我們的實例中使用。這個回呼提供了一個從 SELECT 語句獲得結果的方式。它宣告如下:
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 */ );
如果上面的回呼在sqlite_exec() 程式中作為第三個參數,那麼SQLite 將為SQL 參數內執行的每個SELECT 語句中處理的每個記錄呼叫這個回呼函數。
下面的C 程式碼片段顯示如何從前面建立的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; }
上述程式編譯和執行時,它會產生以下結果:
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
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; }
上述程式編譯和執行時,它會產生以下結果:
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
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; }
上述程式編譯和執行時,它會產生以下結果:
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