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

歡迎訪問 生活随笔!

生活随笔

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

数据库

SQLite学习手册(实例代码一)

發(fā)布時(shí)間:2025/3/12 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLite学习手册(实例代码一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、獲取表的Schema信息:

?? ?1). 動(dòng)態(tài)創(chuàng)建表。
?? ?2). 根據(jù)sqlite3提供的API,獲取表字段的信息,如字段數(shù)量以及每個(gè)字段的類型。
??? 3). 刪除該表。
?? ?見以下代碼及關(guān)鍵性注釋:

1 #include <sqlite3.h> 2 #include <string> 3 4 using namespace std; 5 6 void doTest() 7 { 8 sqlite3* conn = NULL; 9 //1. 打開數(shù)據(jù)庫(kù) 10 int result = sqlite3_open("D:/mytest.db",&conn); 11 if (result != SQLITE_OK) { 12 sqlite3_close(conn); 13 return; 14 } 15 const char* createTableSQL = 16 "CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)"; 17 sqlite3_stmt* stmt = NULL; 18 int len = strlen(createTableSQL); 19 //2. 準(zhǔn)備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對(duì)象,以防止內(nèi)存泄露。 20 if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) { 21 if (stmt) 22 sqlite3_finalize(stmt); 23 sqlite3_close(conn); 24 return; 25 } 26 //3. 通過sqlite3_step命令執(zhí)行創(chuàng)建表的語(yǔ)句。對(duì)于DDL和DML語(yǔ)句而言,sqlite3_step執(zhí)行正確的返回值 27 //只有SQLITE_DONE,對(duì)于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當(dāng)?shù)竭_(dá)結(jié)果集末尾時(shí)則返回 28 //SQLITE_DONE。 29 if (sqlite3_step(stmt) != SQLITE_DONE) { 30 sqlite3_finalize(stmt); 31 sqlite3_close(conn); 32 return; 33 } 34 //4. 釋放創(chuàng)建表語(yǔ)句對(duì)象的資源。 35 sqlite3_finalize(stmt); 36 printf("Succeed to create test table now.\n"); 37 //5. 構(gòu)造查詢表數(shù)據(jù)的sqlite3_stmt對(duì)象。 38 const char* selectSQL = "SELECT * FROM TESTTABLE WHERE 1 = 0"; 39 sqlite3_stmt* stmt2 = NULL; 40 if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),&stmt2,NULL) != SQLITE_OK) { 41 if (stmt2) 42 sqlite3_finalize(stmt2); 43 sqlite3_close(conn); 44 return; 45 } 46 //6. 根據(jù)select語(yǔ)句的對(duì)象,獲取結(jié)果集中的字段數(shù)量。 47 int fieldCount = sqlite3_column_count(stmt2); 48 printf("The column count is %d.\n",fieldCount); 49 //7. 遍歷結(jié)果集中每個(gè)字段meta信息,并獲取其聲明時(shí)的類型。 50 for (int i = 0; i < fieldCount; ++i) { 51 //由于此時(shí)Table中并不存在數(shù)據(jù),再有就是SQLite中的數(shù)據(jù)類型本身是動(dòng)態(tài)的,所以在沒有數(shù)據(jù)時(shí) 52 //無法通過sqlite3_column_type函數(shù)獲取,此時(shí)sqlite3_column_type只會(huì)返回SQLITE_NULL, 53 //直到有數(shù)據(jù)時(shí)才能返回具體的類型,因此這里使用了sqlite3_column_decltype函數(shù)來獲取表聲 54 //明時(shí)給出的聲明類型。 55 string stype = sqlite3_column_decltype(stmt2,i); 56 stype = strlwr((char*)stype.c_str()); 57 //下面的解析規(guī)則見該系列的“數(shù)據(jù)類型-->1. 決定字段親緣性的規(guī)則”部分,其鏈接如下: 58 //http://www.cnblogs.com/stephen-liu74/archive/2012/01/18/2325258.html 59 if (stype.find("int") != string::npos) { 60 printf("The type of %dth column is INTEGER.\n",i); 61 } else if (stype.find("char") != string::npos 62 || stype.find("text") != string::npos) { 63 printf("The type of %dth column is TEXT.\n",i); 64 } else if (stype.find("real") != string::npos 65 || stype.find("floa") != string::npos 66 || stype.find("doub") != string::npos ) { 67 printf("The type of %dth column is DOUBLE.\n",i); 68 } 69 } 70 sqlite3_finalize(stmt2); 71 //8. 為了方便下一次測(cè)試運(yùn)行,我們這里需要?jiǎng)h除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運(yùn)行時(shí)將無法 72 //創(chuàng)建該表,因?yàn)樗呀?jīng)存在。 73 const char* dropSQL = "DROP TABLE TESTTABLE"; 74 sqlite3_stmt* stmt3 = NULL; 75 if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) { 76 if (stmt3) 77 sqlite3_finalize(stmt3); 78 sqlite3_close(conn); 79 return; 80 } 81 if (sqlite3_step(stmt3) == SQLITE_DONE) { 82 printf("The test table has been dropped.\n"); 83 } 84 sqlite3_finalize(stmt3); 85 sqlite3_close(conn); 86 } 87 88 int main() 89 { 90 doTest(); 91 return 0; 92 } 93 //輸出結(jié)果為: 94 //Succeed to create test table now. 95 //The column count is 3. 96 //The type of 0th column is INTEGER. 97 //The type of 1th column is DOUBLE. 98 //The type of 2th column is TEXT. 99 //The test table has been dropped.

二、常規(guī)數(shù)據(jù)插入:

??? 1). 創(chuàng)建測(cè)試數(shù)據(jù)表。
??? 2). 通過INSERT語(yǔ)句插入測(cè)試數(shù)據(jù)。
??? 3). 刪除測(cè)試表。
??? 見以下代碼及關(guān)鍵性注釋:

1 #include <sqlite3.h> 2 #include <string> 3 #include <stdio.h> 4 5 using namespace std; 6 7 void doTest() 8 { 9 sqlite3* conn = NULL; 10 //1. 打開數(shù)據(jù)庫(kù) 11 int result = sqlite3_open("D:/mytest.db",&conn); 12 if (result != SQLITE_OK) { 13 sqlite3_close(conn); 14 return; 15 } 16 const char* createTableSQL = 17 "CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)"; 18 sqlite3_stmt* stmt = NULL; 19 int len = strlen(createTableSQL); 20 //2. 準(zhǔn)備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對(duì)象,以防止內(nèi)存泄露。 21 if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) { 22 if (stmt) 23 sqlite3_finalize(stmt); 24 sqlite3_close(conn); 25 return; 26 } 27 //3. 通過sqlite3_step命令執(zhí)行創(chuàng)建表的語(yǔ)句。對(duì)于DDL和DML語(yǔ)句而言,sqlite3_step執(zhí)行正確的返回值 28 //只有SQLITE_DONE,對(duì)于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當(dāng)?shù)竭_(dá)結(jié)果集末尾時(shí)則返回 29 //SQLITE_DONE。 30 if (sqlite3_step(stmt) != SQLITE_DONE) { 31 sqlite3_finalize(stmt); 32 sqlite3_close(conn); 33 return; 34 } 35 //4. 釋放創(chuàng)建表語(yǔ)句對(duì)象的資源。 36 sqlite3_finalize(stmt); 37 printf("Succeed to create test table now.\n"); 38 39 int insertCount = 10; 40 //5. 構(gòu)建插入數(shù)據(jù)的sqlite3_stmt對(duì)象。 41 const char* insertSQL = "INSERT INTO TESTTABLE VALUES(%d,%f,'%s')"; 42 const char* testString = "this is a test."; 43 char sql[1024]; 44 sqlite3_stmt* stmt2 = NULL; 45 for (int i = 0; i < insertCount; ++i) { 46 sprintf(sql,insertSQL,i,i * 1.0,testString); 47 if (sqlite3_prepare_v2(conn,sql,strlen(sql),&stmt2,NULL) != SQLITE_OK) { 48 if (stmt2) 49 sqlite3_finalize(stmt2); 50 sqlite3_close(conn); 51 return; 52 } 53 if (sqlite3_step(stmt2) != SQLITE_DONE) { 54 sqlite3_finalize(stmt2); 55 sqlite3_close(conn); 56 return; 57 } 58 printf("Insert Succeed.\n"); 59 } 60 sqlite3_finalize(stmt2); 61 //6. 為了方便下一次測(cè)試運(yùn)行,我們這里需要?jiǎng)h除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運(yùn)行時(shí)將無法 62 //創(chuàng)建該表,因?yàn)樗呀?jīng)存在。 63 const char* dropSQL = "DROP TABLE TESTTABLE"; 64 sqlite3_stmt* stmt3 = NULL; 65 if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) { 66 if (stmt3) 67 sqlite3_finalize(stmt3); 68 sqlite3_close(conn); 69 return; 70 } 71 if (sqlite3_step(stmt3) == SQLITE_DONE) { 72 printf("The test table has been dropped.\n"); 73 } 74 sqlite3_finalize(stmt3); 75 sqlite3_close(conn); 76 } 77 78 int main() 79 { 80 doTest(); 81 return 0; 82 } 83 //輸出結(jié)果如下: 84 //Succeed to create test table now. 85 //Insert Succeed. 86 //Insert Succeed. 87 //Insert Succeed. 88 //Insert Succeed. 89 //Insert Succeed. 90 //Insert Succeed. 91 //Insert Succeed. 92 //Insert Succeed. 93 //Insert Succeed. 94 //Insert Succeed. 95 //The test table has been dropped.
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的SQLite学习手册(实例代码一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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