SQLite API
一、SQLite常見API
主要還是記住常見的API,其他的···有需要用到時再查吧
1、打開數(shù)據(jù)庫
函數(shù)原型:int sqlite3_open(const char *,sqlite3 **db)
功能:打開或創(chuàng)建數(shù)據(jù)庫,并通過輸出參數(shù)返回“連接”
參數(shù):
1、數(shù)據(jù)庫文件【IN】
2、sqlite3 數(shù)據(jù)指針【out】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:
2、預(yù)編譯SQL語句
函數(shù)原型:
int sqlite3_prepare(sqlite3*, const char*, int, sqlite3_stmt**,const char**)
功能:預(yù)編譯和解析SQL文本,準(zhǔn)備執(zhí)行
參數(shù):
1、數(shù)據(jù)庫連接指針【IN】
2、sql語句【IN】
3、sql語句最大字符數(shù)【IN】
4、處理后語句statement【out】
5、返回sql語句未使用部分的指針【out】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:
預(yù)編譯+綁定+執(zhí)行+獲取
statement把sql語句解析了的、用sqlite自己標(biāo)記記錄的內(nèi)部數(shù)據(jù)結(jié)構(gòu)。
3、綁定變量
函數(shù)原型:int sqlite3_bind_double(sqlite_stmt *pstmt,int ,double value)
功能:為預(yù)編譯好statement設(shè)定參數(shù)
參數(shù):
1、statement對象【IN】
2、傳入的參數(shù)位置,從1開始編號
3、表示類型,給參數(shù)設(shè)定的值
返回值:錯誤代碼,參見SQLite錯誤代碼
相關(guān)函數(shù):
4、執(zhí)行SQL語句
函數(shù)原型:
int sqlite3_exec(sqlite3 *db,const char *sql, sqlite3_callback, void *, char **errmsg)
功能:執(zhí)行多條或一條SQL語句,并將結(jié)果傳遞給回調(diào)函數(shù)
參數(shù):
1、數(shù)據(jù)庫連接【IN】
2、要執(zhí)行的sql語句【IN】
3、回調(diào)函數(shù) 【IN】
4、傳遞給回調(diào)函數(shù)的參數(shù)地址【IN】
5、返回的錯誤信息【out】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:
5、獲取
5.1、結(jié)果集獲取
函數(shù)原型:int sqlite3_step(sqlite_stmt *pstmt)
功能:執(zhí)行SQL,返回結(jié)果集
參數(shù):statement對象【IN】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:
返回SQLITE_ROW表示準(zhǔn)備好一行的數(shù)據(jù)。
返回SQLITE_DONE表示執(zhí)行完成,無數(shù)據(jù)。
如果只是執(zhí)行SQL,則該函數(shù)只調(diào)用一次即可。
5.2、行數(shù)據(jù)存取
函數(shù)原型:int sqlite3_column_int(sqlite_stmt *pstmt,int col)
功能:獲取某行數(shù)據(jù)中的各列值
參數(shù):
1、pstmt-statement對象【IN】
2、col-列位置,從0開始編號
返回值:返回列值
相關(guān)函數(shù):
5.3、BLOB數(shù)據(jù)獲取
函數(shù)原型:const char *sqlite3_column_blob(sqlite_stmt *pstmt,int col)
功能:獲取某行數(shù)據(jù)中的blob值
參數(shù):
1、pstmt-statement對象【IN】
2、col-列位置,從0開始編號
返回值:返回常量指針,該指針指向blob塊內(nèi)存區(qū)
相關(guān)函數(shù):
5、SQLite3_get_table
函數(shù)原型:
int SQLite3_get_table(sqlite3*, const char *sql, char ***presult, int *nrow, int *ncolumn, char **errmsg)
功能:無需回調(diào)函數(shù)處理,直接查詢結(jié)果集
參數(shù):
1、數(shù)據(jù)庫連接結(jié)構(gòu)【IN】
2、SQL語句【IN】
3、結(jié)果集 【out】
4、行數(shù)【out】
5、列數(shù)【out】
6、錯誤信息【out】
相關(guān)函數(shù):
6、關(guān)閉數(shù)據(jù)庫
函數(shù)原型:int sqlite3_close(sqlite3 *db)
功能:關(guān)閉數(shù)據(jù)庫,釋放資源
參數(shù):1、數(shù)據(jù)庫文件【IN】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:在不與數(shù)據(jù)庫進(jìn)行交互時,關(guān)閉數(shù)據(jù)庫“連接”,并釋放db所指向的內(nèi)存區(qū)。
7、回調(diào)函數(shù)
函數(shù)原型:int 函數(shù)名(void*,int,char**, char**)
功能: sqlite3_exec每查詢到一條記錄調(diào)用一次該回調(diào)函數(shù)
參數(shù):
1、傳入的參數(shù)【IN】
2、結(jié)果集的列數(shù)【IN】
3、列值 【IN】
4、列名【IN】
返回值: 返回給sqlite3_exec的錯誤代碼,務(wù)必返回
二、SQLite錯誤代碼和信息
#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 */1、獲取錯誤代碼
函數(shù)原型:int sqlite3_errcode(sqlite3 *db)
功能:返回最近一次調(diào)用API的錯誤代碼
參數(shù):sqlite3結(jié)構(gòu)指針
返回值:錯誤代碼,參見SQLite錯誤代碼
2、獲取錯誤信息
函數(shù)原型:const char *sqlite3_errmsg(sqlite3 *db)
功能:獲取錯誤信息
參數(shù): sqlite3結(jié)構(gòu)指針
返回值:返回錯誤信息
四、statement對象操作
1、釋放statement對象
函數(shù)原型:int sqlite3_finalize(sqlite_stmt *pstmt)
功能:釋放statement對象資源
參數(shù):statement對象【in】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:該函數(shù)被調(diào)用后,就不能對statement對象和結(jié)果集進(jìn)行操作
注意:不需要記錄集時,需要釋放掉。
2、重置statement對象
函數(shù)原型:int sqlite3_reset(sqlite_stmt *pstmt)
功能:重置statement對象資源
參數(shù):statement對象【in】
返回值:錯誤代碼,參見SQLite錯誤代碼
使用說明:可以重新執(zhí)行語句,再一次從頭到尾獲取結(jié)果集數(shù)據(jù),增加效率,而不是使用預(yù)編譯API函數(shù)。可以重新給statement對象賦參數(shù)值。
注意:如果你需要重復(fù)使用 sqlite3_prepare 解析好的 sqlite3_stmt 結(jié)構(gòu),需要用函數(shù): sqlite3_reset。
如:result = sqlite3_reset(stat);
總結(jié)
以上是生活随笔為你收集整理的SQLite API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习笔记(九)——文件和异常
- 下一篇: python学习笔记(十)——迭代器和生