加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLLite (二) :sqlite3_open, sqlite3_exec, slite3_close

发布时间:2020-12-12 19:45:34 所属栏目:百科 来源:网络整理
导读:导入SQLLite library并引入头文件 libsqlite3.dylib本身是个链接,在这里它指向libsqlite3.0.dylib。 也就是说在这里你添加libsqlite3.dylib和添加libsqlite3.0.dylib其实是添加了同一个文件,没有区别,那为什么要添加libsqlite3.dylib呢? 原因在于libsqli
导入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 plain copy
  1. intsqlite3_open(
  2. constchar*filename,<spanstyle="color:#009900;">/*Databasefilename(UTF-8)*/</span>
  3. sqlite3**ppDb<spanstyle="color:#009900;">/*OUT:SQLitedbhandle*/</span>
  4. );

用这个函数开始数据库操作。需要传入两个参数,一是数据库文件名,比如:比如:E:/test.db. 在ios中可能是:
copy
    NSArray*documentPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  1. NSString*dbPath=[[documentPathobjectAtIndex:0]stringByAppendingPathComponent:@“test.db”];
文件名不需要一定存在,如果此文件不存在,sqlite会自动建立它。如果它存在,就尝试把它当数据库文件来打开。二是sqlite3**,即前面提到的关键数据结构。这个结构底层细节如何,你不要管它。 函数返回值表示操作是否正确,如果是SQLITE_OK则表示操作正常。相关的返回值sqlite定义了一些宏。具体这些宏的含义可以参考sqlite3.h 文件。里面有详细定义(顺便说一下,sqlite3 的代码注释率自称是非常高的,实际上也的确很高。只要你会看英文,sqlite 可以让你学到不少东西)。

关闭数据库链接sqlite3_close用法

原型:

copy
    intsqlite3_close(sqlite3*ppDb);

ppDb为刚才使用sqlite3_open打开的数据库链接

执行sql操作sqlite3_exec用法

原型:

copy
    intsqlite3_exec(
  1. sqlite3*ppDb,<spanstyle="color:#009900;">/*Anopendatabase*/</span>
  2. char*sql,0); background-color:inherit">/*SQLtobeevaluated*/</span>
  3. int(*callback)(void*,int,87); font-weight:bold; background-color:inherit">char**,87); font-weight:bold; background-color:inherit">char**),0); background-color:inherit">/*Callbackfunction*/</span>
  4. /*1stargumenttocallback*/</span>
  5. char**errmsg<spanstyle="color:#009900;">/*Errormsgwrittenhere*/</span>
  6. );
这就是执行一条sql 语句的函数。 第1个参数不再说了,是前面open函数得到的指针。第2个参数constchar*sql是一条sql 语句,以结尾。 第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*值,是一个字段内容(用字符串来表示,以结尾)

//char** column_name 跟column_value是对应的,表示这个字段的字段名称

实例:

copy
    1#include<iostream>
  1. 2usingnamespacestd;
  2. 3#include"sqlite/sqlite3.h"
  3. 4intcallback(char**);
  4. 5intmain()
  5. 6{
  6. 7sqlite3*db;
  7. 8intnResult=sqlite3_open("test.db",&db);
  8. 9if(nResult!=SQLITE_OK)
  9. 10{
  10. 11cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
  11. 12return0;
  12. 13}
  13. 14else
  14. 15{
  15. 16cout<<"数据库打开成功"<<endl;
  16. 17}
  17. 18
  18. 19char*errmsg;
  19. 20
  20. 21nResult=sqlite3_exec(db,"createtableMyTable(idintegerprimarykeyautoincrement,namevarchar(100))",NULL,&errmsg);
  21. 22if(nResult!=SQLITE_OK)
  22. 23{
  23. 24sqlite3_close(db);
  24. 25cout<<errmsg;
  25. 26sqlite3_free(errmsg);
  26. 27return0;
  27. 28}
  28. 29stringstrSql;
  29. 30strSql+="begin;n";
  30. 31for(inti=0;i<100;i++)
  31. 32{
  32. 33strSql+="insertintoMyTablevalues(null,'heh');n";
  33. 34}
  34. 35strSql+="commit;";
  35. 36//cout<<strSql<<endl;
  36. 37
  37. 38nResult=sqlite3_exec(db,strSql.c_str(),&errmsg);
  38. 39
  39. 40 41{
  40. 42sqlite3_close(db);
  41. 43cout<<errmsg<<endl;
  42. 44sqlite3_free(errmsg);
  43. 45 46}
  44. 47
  45. 48strSql="select*fromMyTable";
  46. 49nResult=sqlite3_exec(db,callback,&errmsg);
  47. 50 51{
  48. 52sqlite3_close(db);
  49. 53cout<<errmsg<<endl;
  50. 54sqlite3_free(errmsg);
  51. 55 56}
  52. 57
  53. 58sqlite3_close(db);
  54. 59 60}
  55. 61
  56. 62intnCount,87); font-weight:bold; background-color:inherit">char**pValue,87); font-weight:bold; background-color:inherit">char**pName)
  57. 63{
  58. 64strings;
  59. 65inti=0;i<nCount;i++)
  60. 66{
  61. 67s+=pName[i];
  62. 68s+=":";
  63. 69s+=pValue[i];
  64. 70s+="n";
  65. 71}
  66. 72cout<<s<<endl;
  67. 73 74}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读