国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

sqlite入門基礎(chǔ)

2019-11-11 05:47:06
字體:
供稿:網(wǎng)友

打開數(shù)據(jù)庫鏈接sqlite3_open用法

原型:

int sqlite3_open(  const char *filename,   /* Database filename (UTF-8) */  sqlite3 **ppDb          /* OUT: SQLite db handle */);

用這個(gè)函數(shù)開始數(shù)據(jù)庫操作。需要傳入兩個(gè)參數(shù),一是數(shù)據(jù)庫文件名,比如:E:/test.db。文件名不需要一定存在,如果此文件不存在,sqlite會(huì)自動(dòng)建立它。如果它存在,就嘗試把它當(dāng)數(shù)據(jù)庫文件來打開。二是sqlite3**,即前面提到的關(guān)鍵數(shù)據(jù)結(jié)構(gòu)。這個(gè)結(jié)構(gòu)底層細(xì)節(jié)如何,你不要管它。函數(shù)返回值表示操作是否正確,如果是SQLITE_OK則表示操作正常。相關(guān)的返回值sqlite定義了一些宏。具體這些宏的含義可以參考sqlite3.h 文件。里面有詳細(xì)定義(順便說一下,sqlite3 的代碼注釋率自稱是非常高的,實(shí)際上也的確很高。只要你會(huì)看英文,sqlite 可以讓你學(xué)到不少東西)。

關(guān)閉數(shù)據(jù)庫鏈接sqlite3_close用法

原型:

int sqlite3_close(sqlite3 *ppDb);

ppDb為剛才使用sqlite3_open打開的數(shù)據(jù)庫鏈接

執(zhí)行sql操作sqlite3_exec用法

原型:

int sqlite3_exec(  sqlite3* ppDb,                             /* An open database */  const char *sql,                           /* SQL to be evaluated */  int (*callback)(void*,int,char**,char**),  /* Callback function */  void *,                                    /* 1st argument to callback */  char **errmsg                              /* Error msg written here */);

這就是執(zhí)行一條sql 語句的函數(shù)。第1個(gè)參數(shù)不再說了,是前面open函數(shù)得到的指針。說了是關(guān)鍵數(shù)據(jù)結(jié)構(gòu)。第2個(gè)參數(shù)constchar*sql是一條sql 語句,以/0結(jié)尾。第3個(gè)參數(shù)sqlite3_callback 是回調(diào),當(dāng)這條語句執(zhí)行之后,sqlite3會(huì)去調(diào)用你提供的這個(gè)函數(shù)。第4個(gè)參數(shù)void*是你所提供的指針,你可以傳遞任何一個(gè)指針參數(shù)到這里,這個(gè)參數(shù)最終會(huì)傳到回調(diào)函數(shù)里面,如果不需要傳遞指針給回調(diào)函數(shù),可以填NULL。等下我們?cè)倏椿卣{(diào)函數(shù)的寫法,以及這個(gè)參數(shù)的使用。第5個(gè)參數(shù)char** errmsg 是錯(cuò)誤信息。注意是指針的指針。sqlite3里面有很多固定的錯(cuò)誤信息。執(zhí)行sqlite3_exec 之后,執(zhí)行失敗時(shí)可以查閱這個(gè)指針(直接cout<<errmsg得到一串字符串信息,這串信息告訴你錯(cuò)在什么地方。sqlite3_exec函數(shù)通過修改你傳入的指針的指針,把你提供的指針指向錯(cuò)誤提示信息,這樣sqlite3_exec函數(shù)外面就可以通過這個(gè)char*得到具體錯(cuò)誤提示。說明:通常,sqlite3_callback 和它后面的void*這兩個(gè)位置都可以填NULL。填NULL表示你不需要回調(diào)。比如你做insert 操作,做delete操作,就沒有必要使用回調(diào)。而當(dāng)你做select 時(shí),就要使用回調(diào),因?yàn)閟qlite3 把數(shù)據(jù)查出來,得通過回調(diào)告訴你查出了什么數(shù)據(jù)。

exec 的回調(diào)

typedef int(*sqlite3_callback)(void*,int,char**,char**);你的回調(diào)函數(shù)必須定義成上面這個(gè)函數(shù)的類型。下面給個(gè)簡單的例子://sqlite3的回調(diào)函數(shù)//sqlite 每查到一條記錄,就調(diào)用一次這個(gè)回調(diào)int LoadMyInfo(void* para,intn_column,char** column_value,char** column_name);

//para是你在sqlite3_exec 里傳入的void*參數(shù)通過para參數(shù),你可以傳入一些特殊的指針(比如類指針、結(jié)構(gòu)指針),//然后在這里面強(qiáng)制轉(zhuǎn)換成對(duì)應(yīng)的類型(這里面是void*類型,必須強(qiáng)制轉(zhuǎn)換成你的類型才可用)。然后操作這些數(shù)據(jù)//n_column是這一條記錄有多少個(gè)字段(即這條記錄有多少列)//char** column_value 是個(gè)關(guān)鍵值,查出來的數(shù)據(jù)都保存在這里,它實(shí)際上是個(gè)1維數(shù)組(不要以為是2維數(shù)組),//每一個(gè)元素都是一個(gè)char*值,是一個(gè)字段內(nèi)容(用字符串來表示,以/0結(jié)尾)//char** column_name 跟column_value是對(duì)應(yīng)的,表示這個(gè)字段的字段名稱

實(shí)例:

#include <iostream>using namespace std;#include "sqlite/sqlite3.h"int callback(void*,int,char**,char**);int main(){    sqlite3* db;    int nResult = sqlite3_open("test.db",&db);    if (nResult != SQLITE_OK)    {        cout<<"打開數(shù)據(jù)庫失?。?quot;<<sqlite3_errmsg(db)<<endl;        return 0;    }    else    {        cout<<"數(shù)據(jù)庫打開成功"<<endl;    }    char* errmsg;    nResult = sqlite3_exec(db,"create table fuck(id integer PRimary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);     if (nResult != SQLITE_OK)     {         sqlite3_close(db);         cout<<errmsg;         sqlite3_free(errmsg);        return 0;    }    string strSql;    strSql+="begin;/n";    for (int i=0;i<100;i++)    {        strSql+="insert into fuck values(null,'heh');/n";    }    strSql+="commit;";    //cout<<strSql<<endl;    nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);    if (nResult != SQLITE_OK)    {        sqlite3_close(db);        cout<<errmsg<<endl;        sqlite3_free(errmsg);        return 0;    }    strSql = "select * from fuck";    nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);      if (nResult != SQLITE_OK)    {        sqlite3_close(db);        cout<<errmsg<<endl;        sqlite3_free(errmsg);        return 0;    }    sqlite3_close(db);    return 0;}int callback(void* ,int nCount,char** pValue,char** pName){    string s;    for(int i=0;i<nCount;i++)    {        s+=pName[i];        s+=":";        s+=pValue[i];        s+="/n";    }    cout<<s<<endl;    return 0;}

上一篇介紹的sqlite3_exec 是使用回調(diào)來執(zhí)行對(duì)select結(jié)果的操作。還有一個(gè)方法可以直接查詢而不需要回調(diào)。但是,我個(gè)人感覺還是回調(diào)好,因?yàn)榇a可以更加整齊,只不過用回調(diào)很麻煩,你得聲明一個(gè)函數(shù),如果這個(gè)函數(shù)是類成員函數(shù),你還不得不把它聲明成static的(要問為什么?這又是C++基礎(chǔ)了。C++成員函數(shù)實(shí)際上隱藏了一個(gè)參數(shù):this,C++調(diào)用類的成員函數(shù)的時(shí)候,隱含把類指針當(dāng)成函數(shù)的第一個(gè)參數(shù)傳遞進(jìn)去。結(jié)果,這造成跟前面說的sqlite 回調(diào)函數(shù)的參數(shù)不相符。只有當(dāng)把成員函數(shù)聲明成static 時(shí),它才沒有多余的隱含的this參數(shù))。雖然回調(diào)顯得代碼整齊,但有時(shí)候你還是想要非回調(diào)的select 查詢。這可以通過sqlite3_get_table 函數(shù)做到。

int sqlite3_get_table(  sqlite3 *db,          /* An open database */  const char *zSql,     /* SQL to be evaluated */  char ***pazResult,    /* Results of the query */  int *pnRow,           /* Number of result rows written here */  int *pnColumn,        /* Number of result columns written here */  char **pzErrmsg       /* Error msg written here */);void sqlite3_free_table(char **result);

第1個(gè)參數(shù)不再多說,看前面的例子。第2個(gè)參數(shù)是sql 語句,跟sqlite3_exec 里的sql 是一樣的。是一個(gè)很普通的以/0結(jié)尾的char*字符串。第3個(gè)參數(shù)是查詢結(jié)果,它依然一維數(shù)組(不要以為是二維數(shù)組,更不要以為是三維數(shù)組)。它內(nèi)存布局是:字段名稱,后面是緊接著是每個(gè)字段的值。下面用例子來說事。第4個(gè)參數(shù)是查詢出多少條記錄(即查出多少行,不包括字段名那行)。第5個(gè)參數(shù)是多少個(gè)字段(多少列)。第6個(gè)參數(shù)是錯(cuò)誤信息,跟前面一樣,這里不多說了。

pazResult返回的字符串?dāng)?shù)量實(shí)際上是(*pnRow+1)*(*pnColumn),因?yàn)榍?*pnColumn)個(gè)是字段名

修改上篇的例子,使用sqlite3_get_table,來去的結(jié)果集:

#include <iostream>using namespace std;#include "sqlite/sqlite3.h"int callback(void*,int,char**,char**);int main(){    sqlite3* db;    int nResult = sqlite3_open("test.db",&db);    if (nResult != SQLITE_OK)    {        cout<<"打開數(shù)據(jù)庫失?。?quot;<<sqlite3_errmsg(db)<<endl;        return 0;    }    else    {        cout<<"數(shù)據(jù)庫打開成功"<<endl;    }    char* errmsg;    nResult = sqlite3_exec(db,"create table fuck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);     if (nResult != SQLITE_OK)     {         sqlite3_close(db);         cout<<errmsg;         sqlite3_free(errmsg);        return 0;    }    string strSql;    strSql+="begin;/n";    for (int i=0;i<100;i++)    {        strSql+="insert into fuck values(null,'heh');/n";    }    strSql+="commit;";    //cout<<strSql<<endl;    nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);    if (nResult != SQLITE_OK)    {        sqlite3_close(db);        cout<<errmsg<<endl;        sqlite3_free(errmsg);        return 0;    }    strSql = "select * from fuck";    //nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);    char** pResult;    int nRow;    int nCol;    nResult = sqlite3_get_table(db,strSql.c_str(),&pResult,&nRow,&nCol,&errmsg);      if (nResult != SQLITE_OK)    {        sqlite3_close(db);        cout<<errmsg<<endl;        sqlite3_free(errmsg);        return 0;    }    string strOut;    int nIndex = nCol;    for(int i=0;i<nRow;i++)    {        for(int j=0;j<nCol;j++)        {            strOut+=pResult[j];            strOut+=":";            strOut+=pResult[nIndex];            strOut+="/n";            ++nIndex;        }    }    sqlite3_free_table(pResult);    cout<<strOut<<endl;    sqlite3_close(db);    return 0;}/*int callback(void* ,int nCount,char** pValue,char** pName){    string s;    for(int i=0;i<nCount;i++)    {        s+=pName[i];        s+=":";        s+=pValue[i];        s+="/n";    }    cout<<s<<endl;    return 0;}*/


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄梅县| 柯坪县| 大宁县| 宝应县| 乡城县| 海南省| 美姑县| 任丘市| 桐乡市| 龙胜| 长乐市| 米泉市| 迁西县| 鄂州市| 洛南县| 临漳县| 多伦县| 登封市| 四川省| 吴桥县| 渭源县| 开远市| 北流市| 湘乡市| 福清市| 嘉峪关市| 白银市| 柯坪县| 峨眉山市| 婺源县| 延安市| 宁城县| 龙川县| 葵青区| 额尔古纳市| 南投县| 林口县| 永济市| 安阳市| 阳原县| 沅江市|