SQLLite (三):sqlite3_get_table,sqlite3_free_table
生活随笔
收集整理的這篇文章主要介紹了
SQLLite (三):sqlite3_get_table,sqlite3_free_table
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇介紹的sqlite3_exec 是使用回調來執行對select結果的操作,你得聲明一個函數,如果這個函數是類成員函數,你還不得不把它聲明成static的(要問為什么?這又是C++基礎了。C++成員函數實際上隱藏了一個參數:this,C++調用類的成員函數的時候,隱含把類指針當成函數的第一個參數傳遞進去。結果,這造成跟前面說的sqlite 回調函數的參數不相符。只有當把成員函數聲明成static 時,它才沒有多余的隱含的this參數)。
1?int?sqlite3_get_table(?? 2???sqlite3?*db,??????????<span?style="color:#009900;">/*?An?open?database?*/</span>?? 3???const?char?*zSql,?????<span?style="color:#009900;">/*?SQL?to?be?evaluated?*/</span>?? 4???char?***pazResult,????<span?style="color:#009900;">/*?Results?of?the?query?*/</span>?? 5???int?*pnRow,???????????<span?style="color:#009900;">/*?Number?of?result?rows?written?here?*/</span>?? 6???int?*pnColumn,????????<span?style="color:#009900;">/*?Number?of?result?columns?written?here?*/</span>?? 7???char?**pzErrmsg???????<span?style="color:#009900;">/*?Error?msg?written?here?*/</span>?? 8?);?? 9?void?sqlite3_free_table(char?**result);?? #include?<iostream>?? ?2?using?namespace?std;?? ?3?#include?"sqlite/sqlite3.h"?? ?4?int?callback(void*,int,char**,char**);?? ?5?int?main()?? ?6?{?? ?7?????sqlite3*?db;?? ?8?????int?nResult?=?sqlite3_open("test.db",&db);?? ?9?????if?(nResult?!=?SQLITE_OK)?? 10?????{?? 11?????????cout<<"打開數據庫失敗:"<<sqlite3_errmsg(db)<<endl;?? 12?????????return?0;?? 13?????}?? 14?????else?? 15?????{?? 16?????????cout<<"數據庫打開成功"<<endl;?? 17?????}?? 18??? 19?????char*?errmsg;?? 20??? 21?????nResult?=?sqlite3_exec(db,"create?table?MyTable(id?integer?primary?key?autoincrement,name?varchar(100))",NULL,NULL,&errmsg);?? 22??????if?(nResult?!=?SQLITE_OK)?? 23??????{?? 24??????????sqlite3_close(db);?? 25??????????cout<<errmsg;?? 26??????????sqlite3_free(errmsg);?? 27?????????return?0;?? 28?????}?? 29?????string?strSql;?? 30?????strSql+="begin;\n";?? 31?????for?(int?i=0;i<100;i++)?? 32?????{?? 33?????????strSql+="insert?into?MyTable?values(null,'heh');\n";?? 34?????}?? 35?????strSql+="commit;";?? 36?????//cout<<strSql<<endl;?? 37??? 38?????nResult?=?sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);?? 39??? 40?????if?(nResult?!=?SQLITE_OK)?? 41?????{?? 42?????????sqlite3_close(db);?? 43?????????cout<<errmsg<<endl;?? 44?????????sqlite3_free(errmsg);?? 45?????????return?0;?? 46?????}?? 47??? 48?????strSql?=?"select?*?from?MyTable";?? <span?style="color:#009900;">49?????//nResult?=?sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);</span>?? 50?????char**?pResult;?? 51?????int?nRow;?? 52?????int?nCol;?? 53?????nResult?=?sqlite3_get_table(db,strSql.c_str(),&pResult,&nRow,&nCol,&errmsg);?? 54???????if?(nResult?!=?SQLITE_OK)?? 55?????{?? 56?????????sqlite3_close(db);?? 57?????????cout<<errmsg<<endl;?? 58?????????sqlite3_free(errmsg);?? 59?????????return?0;?? 60?????}?? 61??? 62?????string?strOut;?? 63?????int?nIndex?=?nCol;?? 64?????for(int?i=0;i<nRow;i++)?? 65?????{?? 66?????????for(int?j=0;j<nCol;j++)?? 67?????????{?? 68?????????????strOut+=pResult[j];?? 69?????????????strOut+=":";?? 70?????????????strOut+=pResult[nIndex];?? 71?????????????strOut+="\n";?? 72?????????????++nIndex;?? 73?????????}?? 74?????}?? 75?????sqlite3_free_table(pResult);?? 76?????cout<<strOut<<endl;?? 77?????sqlite3_close(db);?? 78?????return?0;?? 79?}?? <span?style="color:#009900;">80?/*? 81?int?callback(void*?,int?nCount,char**?pValue,char**?pName)? 82?{? 83?????string?s;? 84?????for(int?i=0;i<nCount;i++)? 85?????{? 86?????????s+=pName[i];? 87?????????s+=":";? 88?????????s+=pValue[i];? 89?????????s+="\n";? 90?????}? 91?????cout<<s<<endl;? 92?????return?0;? 93?}*/</span> ?
有時候你還是想要非回調的select 查詢。這可以通過sqlite3_get_table 函數做到。
[cpp]?view plaincopy
第1個參數不再多說,看前面的例子。 第2個參數是sql 語句,跟sqlite3_exec 里的sql 是一樣的。是一個很普通的以\0結尾的char*字符串。 第3個參數是查詢結果,它依然一維數組(不要以為是二維數組,更不要以為是三維數組)。它內存布局是:字段名稱,后面是緊接著是每個字段的值。下面用例子來說事。 第4個參數是查詢出多少條記錄(即查出多少行,不包括字段名那行)。 第5個參數是多少個字段(多少列)。 第6個參數是錯誤信息,跟前面一樣,這里不多說了。
pazResult返回的字符串數量實際上是(*pnRow+1)*(*pnColumn),因為前(*pnColumn)個是字段名
修改上篇的例子,使用sqlite3_get_table,來去的結果集:
[cpp]?view plaincopy總結
以上是生活随笔為你收集整理的SQLLite (三):sqlite3_get_table,sqlite3_free_table的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQLLite (二) :sqlite3
- 下一篇: SQLLite (四):sqlite3_