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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

sqlite3 c语言编程,SQLite教程(十三):C语言编程实例代码(1)

發布時間:2024/9/18 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqlite3 c语言编程,SQLite教程(十三):C语言编程实例代码(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#include

#include

using namespace std;

void doTest()

{

sqlite3* conn = NULL;

//1. 打開數據庫

int result = sqlite3_open("D:/mytest.db",&conn);

if (result != SQLITE_OK) {

sqlite3_close(conn);

return;

}

const char* createTableSQL =

"CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";

sqlite3_stmt* stmt = NULL;

int len = strlen(createTableSQL);

//2. 準備創建數據表,如果創建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對象,以防止內存泄露。

if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {

if (stmt)

sqlite3_finalize(stmt);

sqlite3_close(conn);

return;

}

//3. 通過sqlite3_step命令執行創建表的語句。對于DDL和DML語句而言,sqlite3_step執行正確的返回值

//只有SQLITE_DONE,對于SELECT查詢而言,如果有數據返回SQLITE_ROW,當到達結果集末尾時則返回

//SQLITE_DONE。

if (sqlite3_step(stmt) != SQLITE_DONE) {

sqlite3_finalize(stmt);

sqlite3_close(conn);

return;

}

//4. 釋放創建表語句對象的資源。

sqlite3_finalize(stmt);

printf("Succeed to create test table now.\n");

//5. 構造查詢表數據的sqlite3_stmt對象。

const char* selectSQL = "SELECT * FROM TESTTABLE WHERE 1 = 0";

sqlite3_stmt* stmt2 = NULL;

if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),&stmt2,NULL) != SQLITE_OK) {

if (stmt2)

sqlite3_finalize(stmt2);

sqlite3_close(conn);

return;

}

//6. 根據select語句的對象,獲取結果集中的字段數量。

int fieldCount = sqlite3_column_count(stmt2);

printf("The column count is %d.\n",fieldCount);

//7. 遍歷結果集中每個字段meta信息,并獲取其聲明時的類型。

for (int i = 0; i < fieldCount; ++i) {

//由于此時Table中并不存在數據,再有就是SQLite中的數據類型本身是動態的,所以在沒有數據時

//無法通過sqlite3_column_type函數獲取,此時sqlite3_column_type只會返回SQLITE_NULL,

//直到有數據時才能返回具體的類型,因此這里使用了sqlite3_column_decltype函數來獲取表聲

//明時給出的聲明類型。

string stype = sqlite3_column_decltype(stmt2,i);

stype = strlwr((char*)stype.c_str());

//下面的解析規則見該系列的“數據類型-->1. 決定字段親緣性的規則”部分,其鏈接如下:

//https://www.jb51.net/article/65424.htm

if (stype.find("int") != string::npos) {

printf("The type of %dth column is INTEGER.\n",i);

} else if (stype.find("char") != string::npos

|| stype.find("text") != string::npos) {

printf("The type of %dth column is TEXT.\n",i);

} else if (stype.find("real") != string::npos

|| stype.find("floa") != string::npos

|| stype.find("doub") != string::npos ) {

printf("The type of %dth column is DOUBLE.\n",i);

}

}

sqlite3_finalize(stmt2);

//8. 為了方便下一次測試運行,我們這里需要刪除該函數創建的數據表,否則在下次運行時將無法

//創建該表,因為它已經存在。

const char* dropSQL = "DROP TABLE TESTTABLE";

sqlite3_stmt* stmt3 = NULL;

if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) {

if (stmt3)

sqlite3_finalize(stmt3);

sqlite3_close(conn);

return;

}

if (sqlite3_step(stmt3) == SQLITE_DONE) {

printf("The test table has been dropped.\n");

}

sqlite3_finalize(stmt3);

sqlite3_close(conn);

}

int main()

{

doTest();

return 0;

}

//輸出結果為:

//Succeed to create test table now.

//The column count is 3.

//The type of 0th column is INTEGER.

//The type of 1th column is DOUBLE.

//The type of 2th column is TEXT.

//The test table has been dropped.

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的sqlite3 c语言编程,SQLite教程(十三):C语言编程实例代码(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。