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

歡迎訪問 生活随笔!

生活随笔

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

数据库

SQLLite (二) :sqlite3_open, sqlite3_exec, slite3_close

發布時間:2024/4/11 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLLite (二) :sqlite3_open, sqlite3_exec, slite3_close 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
導入SQLLite library并引入頭文件

libsqlite3.dylib本身是個鏈接,在這里它指向libsqlite3.0.dylib。也就是說在這里你添加libsqlite3.dylib和添加libsqlite3.0.dylib其實是添加了同一個文件,沒有區別,那為什么要添加libsqlite3.dylib呢?原因在于libsqlite3.dylib總是指向最新的sqlite3動態庫,也就是說如果出現了新的動態庫(如:libsqlite3.1.dylib)那libsqlite3.dylib將指向這個新的動態庫(libsqlite3.1.dylib)而不在是libsqlite3.0.dylib了!所以建議還是要添加libsqlite3.dylib。

注:

On Mac OS X, frameworks are just libraries, packed into a bundle. Within the bundle you will find an actual dynamic library (libWhatever.dylib). The difference between a bare library and the framework on Mac is that a framework can contain multiple different versions of the library. It can contain extra resources (images, localized strings, XML data files, UI objects, etc.) and unless the framework is released to public, it usually contains the necessary .h files you need to use the library.

A library is just that, "a library". It is a collection of objects/functions/methods (depending on your language) and your application "links" against it and thus can use the objects/functions/methods. It is basically a file containing re-usable code that can usually be shared among multiple applications (you don't have to write the same code over and over again).


打開數據庫鏈接sqlite3_open用法

原型:

[cpp]?view plaincopy
  • int?sqlite3_open(??
  • ??const?char?*filename,???<span?style="color:#009900;">/*?Database?filename?(UTF-8)?*/</span>??
  • ??sqlite3?**ppDb??????????<span?style="color:#009900;">/*?OUT:?SQLite?db?handle?*/</span>??
  • );??

  • 用這個函數開始數據庫操作。需要傳入兩個參數,一是數據庫文件名,比如:比如:E:/test.db. 在ios中可能是:
    [cpp]?view plaincopy
  • NSArray?*?documentPath?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);??
  • NSString?*?dbPath?=?[[documentPath?objectAtIndex:0]?stringByAppendingPathComponent:@“test.db”];??
  • 文件名不需要一定存在,如果此文件不存在,sqlite會自動建立它。如果它存在,就嘗試把它當數據庫文件來打開。二是sqlite3**,即前面提到的關鍵數據結構。這個結構底層細節如何,你不要管它。 函數返回值表示操作是否正確,如果是SQLITE_OK則表示操作正常。相關的返回值sqlite定義了一些宏。具體這些宏的含義可以參考sqlite3.h 文件。里面有詳細定義(順便說一下,sqlite3 的代碼注釋率自稱是非常高的,實際上也的確很高。只要你會看英文,sqlite 可以讓你學到不少東西)。

    關閉數據庫鏈接sqlite3_close用法

    原型:

    [cpp]?view plaincopy
  • int?sqlite3_close(sqlite3?*ppDb);??
  • ppDb為剛才使用sqlite3_open打開的數據庫鏈接

    執行sql操作sqlite3_exec用法

    原型:

    [cpp]?view plaincopy
  • int?sqlite3_exec(??
  • ??sqlite3*?ppDb,?????????????????????????????<span?style="color:#009900;">/*?An?open?database?*/</span>??
  • ??const?char?*sql,???????????????????????????<span?style="color:#009900;">/*?SQL?to?be?evaluated?*/</span>??
  • ??int?(*callback)(void*,int,char**,char**),??<span?style="color:#009900;">/*?Callback?function?*/</span>??
  • ??void?*,????????????????????????????????????<span?style="color:#009900;">/*?1st?argument?to?callback?*/</span>??
  • ??char?**errmsg?????????????????????????????<span?style="color:#009900;">?/*?Error?msg?written?here?*/</span>??
  • );??
  • 這就是執行一條sql 語句的函數。 第1個參數不再說了,是前面open函數得到的指針。第2個參數constchar*sql是一條sql 語句,以\0結尾。 第3個參數sqlite3_callback 是回調,當這條語句執行之后,sqlite3會去調用你提供的這個函數。 第4個參數void*是你所提供的指針,你可以傳遞任何一個指針參數到這里,這個參數最終會傳到回調函數里面,如果不需要傳遞指針給回調函數,可以填NULL。等下我們再看回調函數的寫法,以及這個參數的使用。 第5個參數char** errmsg 是錯誤信息。注意是指針的指針。sqlite3里面有很多固定的錯誤信息。執行sqlite3_exec 之后,執行失敗時可以查閱這個指針(直接cout<<errmsg得到一串字符串信息,這串信息告訴你錯在什么地方。sqlite3_exec函數通過修改你傳入的指針的指針,把你提供的指針指向錯誤提示信息,這樣sqlite3_exec函數外面就可以通過這個char*得到具體錯誤提示。
    說明:通常,sqlite3_callback 和它后面的void*這兩個位置都可以填NULL。填NULL表示你不需要回調。比如你做insert 操作,做delete操作,就沒有必要使用回調。而當你做select 時,就要使用回調,因為sqlite3 把數據查出來,得通過回調告訴你查出了什么數據。

    exec 的回調

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

    //para是你在sqlite3_exec 里傳入的void*參數通過para參數,你可以傳入一些特殊的指針(比如類指針、結構指針), 然后在這里面強制轉換成對應的類型(這里面是void*類型,必須強制轉換成你的類型才可用)。然后操作這些數據?

    //n_column是這一條記錄有多少個字段(即這條記錄有多少列)?

    //char** column_value 是個關鍵值,查出來的數據都保存在這里,它實際上是個1維數組(不要以為是2維數組), 每一個元素都是一個char*值,是一個字段內容(用字符串來表示,以\0結尾)?

    //char** column_name 跟column_value是對應的,表示這個字段的字段名稱

    實例:

    [cpp]?view plaincopy
  • ?1?#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";??
  • 49?????nResult?=?sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);??
  • 50???????if?(nResult?!=?SQLITE_OK)??
  • 51?????{??
  • 52?????????sqlite3_close(db);??
  • 53?????????cout<<errmsg<<endl;??
  • 54?????????sqlite3_free(errmsg);??
  • 55?????????return?0;??
  • 56?????}??
  • 57???
  • 58?????sqlite3_close(db);??
  • 59?????return?0;??
  • 60?}??
  • 61???
  • 62?int?callback(void*?,int?nCount,char**?pValue,char**?pName)??
  • 63?{??
  • 64?????string?s;??
  • 65?????for(int?i=0;i<nCount;i++)??
  • 66?????{??
  • 67?????????s+=pName[i];??
  • 68?????????s+=":";??
  • 69?????????s+=pValue[i];??
  • 70?????????s+="\n";??
  • 71?????}??
  • 72?????cout<<s<<endl;??
  • 73?????return?0;??
  • 74?}?
  • 總結

    以上是生活随笔為你收集整理的SQLLite (二) :sqlite3_open, sqlite3_exec, slite3_close的全部內容,希望文章能夠幫你解決所遇到的問題。

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