日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLite API

發(fā)布時間:2025/3/21 数据库 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLite API 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、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錯誤代碼
使用說明:

typedef struct sqlite3 sqlite3; 頭文件 #include <sqlite3.h>

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錯誤代碼
使用說明:

//typedef struct sqlite3_stmt sqlite3_stmt; sqlite3_stmt *pstmt; sqlite3_prepare(db,”select * from tbl_test”,60,&pstmt,0);

預(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ù):

int sqlite3_bind_int(sqlite3_stmt*, int, int); int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)) int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));sqlite3_bind_text(stmt, 1, “xiaohong”, 8, SQLITE_STATIC); 其中第四個參數(shù)指字符串的長度,不包括\0。 第五個參數(shù)是一個函數(shù)指針,sqlite3執(zhí)行完操作后回調(diào)此函數(shù),通常用于釋放字符串所占的內(nèi)存。 SQLITE_STATIC就是告訴sqlite3字符串是常量,可以放心使用。 也可以使用SQLITE_TRANSIENT ,使得該函數(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錯誤代碼
使用說明:

typedef int (*sqlite3_callback)(void*,int,char**, char**);

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ù):

int sqlite3_column_count(sqlite3_stmt*); const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); double sqlite3_column_double(sqlite3_stmt*, int iCol);

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ù):

int sqlite3_column_bytes(sqlite3_stmt*,int iCol);//返回字段值長度

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ù):

sqlite3_free_table(char **presult);

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。