SQLite C/C++ 教程

目錄html

安裝code

在咱們開始使用SQLiteC / C++程序,咱們須要確保SQLite庫設置的機器上。您能夠檢查SQLite安裝章瞭解安裝過程sqlite

 

詳情請見:http://www.cnblogs.com/BoyXiao/archive/2012/03/31/2426495.html

 

C/C++ Interface APIs

如下是重要的C&C + + / SQLite接口程序能夠知足你的要求使用SQLite數據庫從您的C / C++程序。若是你正在尋找一個更復雜的應用程序,而後你能夠看看SQLite官方文檔。

 

S.N 

API 

sqlite3_open(const char *filename, sqlite3 **ppDb)

 

這個例程打開一個鏈接到SQLite數據庫的文件,並返回一個數據庫鏈接對象被其餘使用SQLite例程。

 

若是文件名參數是NULL或內存:,sqlite3_open()將建立一個內存數據庫在RAM,只持續時間的會議。

 

若是文件名不是NULL,sqlite3_open()嘗試打開數據庫文件經過使用它的價值。若是沒有叫這個名字的文件存在,sqlite3_open()將打開一個新的數據庫文件的名稱。

sqlite3_exec(sqlite3*,const char *sql, sqlite_callback, void *data, char **errmsg)

 

這個程序提供了一種快速、簡單的方法來執行SQL命令提供的SQL參數能夠包含一個以上的SQL命令。

 

這裏,第一個參數sqlite3開放數據庫對象,sqlite_callback回調數據是第一參數和errmsg將返回來捕獲任何錯誤提出的例行公事。

 

sqlite3_exec()例程解析並執行每一個命令的sql參數,直到到達結束的字符串或遇到錯誤。

sqlite3_close(sqlite3*)

 

這個例程關閉數據庫鏈接調用sqlite3_open以前打開的()。與鏈接相關聯的全部準備好的語句應在關閉鏈接以前完成。

 

若是任何疑問仍然沒有完成,sqlite3_close()將返回SQLITE_BUSY因爲unfinalized語句錯誤消息沒法關閉。

 

如下代碼要求提示:

    每一例子建立的數據庫,都是基於前一個數據庫建立的。不然如下代碼均不能運行!!

鏈接到數據庫

C代碼片斷展現瞭如何鏈接到現有的數據庫。若是數據庫不存在,那麼它將被建立並最終將返回一個數據庫對象。

#include <stdio.h>

#include <stdlib.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);

}

如今,讓咱們來編譯和運行程序來建立咱們的數據庫測試之上。db在當前目錄。你能夠改變你的路徑按您的需求。

$gcc test.c -l sqlite3

$./a.out

Opened database successfully

 

若是你要使用c++源代碼,而後你能夠編譯代碼以下:

$g++ test.c -l sqlite3

 

咱們鏈接咱們的程序與sqlite3圖書館提供所需功能的C程序。這將建立一個數據庫文件測試。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");

}

 

/*** 產生SQL語句 ***/

sql = "CREATE TABLE COMPANY(" \

"ID INT PRIMARY KEY NOT NULL," \

"NAME TEXT NOT NULL," \

"AGE INT NOT NULL," \

"ADDRESS CHAR(50)," \

"SALARY REAL );";

 

/*** 執行SQLite語句***/

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;

}

 

以上程序編譯和執行時,它將建立公司在您的測試表。db和最終的清單文件將以下:

-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

 

插入操做

這段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(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;

}

以上程序編譯和執行時,它將建立鑑於公司表和記錄將顯示如下兩行:

Opened database successfully
Records created successfully

 

查詢操做

在咱們繼續進行以前,獲取記錄,與實際例子讓我給一個小細節的回調函數,咱們使用咱們的例子。這個回調提供了一種方法來獲取SELECT語句的結果。它具備如下聲明:

 

typedefint(*sqlite3_callback)(

void*,/*提供sqlite3_exec 4參數的數據() */

int,/*行中的列數 */

char**,/*表明行中的字段的字符串數組*/

char**/*一個字符串數組表明列名 */

);

若是上面的回調提供了sqlite_exec做爲第三個參數()例程,SQLite將爲每一個記錄調用這個回調函數處理每一個SELECT語句內執行SQL參數。

 

C代碼段顯示咱們如何獲取和顯示記錄從咱們公司表中建立上面的例子:

#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 = 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 

 

更新操做

C代碼片斷展現瞭如何使用UPDATE語句更新任何記錄,而後獲取和顯示從咱們公司表更新記錄:

#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;

}

 

刪除操做

C代碼片斷展現瞭如何使用DELETE語句刪除任何記錄,而後獲取並顯示剩餘記錄從咱們公司表:

#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
相關文章
相關標籤/搜索