sqlite入门基础

前端之家收集整理的这篇文章主要介绍了sqlite入门基础前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

源地址:http://www.cnblogs.com/likebeta/category/388368.html

打开数据库链接sqlite3_open用法

原型:

  1. int sqlite3_open(
  2. const char *filename,/* Database filename (UTF-8) */
  3. sqlite3 **ppDb OUT: sqlite db handle */
  4. );

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

关闭数据库链接sqlite3_close用法

int sqlite3_close(sqlite3 *ppDb);

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

执行sql操作sqlite3_exec用法

int sqlite3_exec( sqlite3* ppDb,0); line-height:1.5!important"> An open database */ char *sql,0); line-height:1.5!important"> sql to be evaluated int (*callback)(void*,255); line-height:1.5!important">int,255); line-height:1.5!important">char**,255); line-height:1.5!important">char**),0); line-height:1.5!important"> Callback function void *,0); line-height:1.5!important"> 1st argument to callback char **errmsg Error msg written here 这就是执行一条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,intn_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是对应的,表示这个字段的字段名称

实例:

  1. #include <iostream>
  2. using namespace std;
  3. #include "sqlite/sqlite3.h"
  4. int callback(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. char* errmsg;
  19. nResult = sqlite3_exec(db,create table fuck(id integer primary key autoincrement,name varchar(100))errmsg);
  20. if (nResult != sqlITE_OK)
  21. {
  22. sqlite3_close(db);
  23. cout<<errmsg;
  24. sqlite3_free(errmsg);
  25. string strsql;
  26. strsql+=begin;\n";
  27. for (int i=0;i<100;i++)
  28. {
  29. strsql+=insert into fuck values(null,'heh');\n";
  30. }
  31. strsql+=commit;//cout<<strsql<<endl;
  32. nResult = sqlite3_exec(db,strsql.c_str(),&errmsg);
  33. if (nResult != sqlITE_OK)
  34. {
  35. sqlite3_close(db);
  36. cout<<errmsg<<endl;
  37. sqlite3_free(errmsg);
  38. 0;
  39. }
  40. strsql = select * from fuck";
  41. nResult = sqlite3_exec(db,callback,&errmsg);
  42. 0;
  43. }
  44. sqlite3_close(db);
  45. 0;
  46. }
  47. int nCount,255); line-height:1.5!important">char** pValue,255); line-height:1.5!important">char** pName)
  48. {
  49. string s;
  50. for(0;i<nCount;i++)
  51. {
  52. s+=pName[i];
  53. s+=:";
  54. s+=pValue[i];
  55. s+=\n";
  56. }
  57. cout<<s<<endl;
  58. 0;
  59. }


上一篇介绍的sqlite3_exec 是使用回调来执行对select结果的操作。还有一个方法可以直接查询而不需要回调。但是,我个人感觉还是回调好,因为代码可以更加整齐,只不过用回调很麻烦,你得声明一个函数,如果这个函数是类成员函数,你还不得不把它声明成static的(要问为什么?这又是C++基础了。C++成员函数实际上隐藏了一个参数:this,C++调用类的成员函数的时候,隐含把类指针当成函数的第一个参数传递进去。结果,这造成跟前面说的sqlite 回调函数的参数不相符。只有当把成员函数声明成static 时,它才没有多余的隐含的this参数)。
虽然回调显得代码整齐,但有时候你还是想要非回调的select 查询。这可以通过sqlite3_get_table 函数做到。

int sqlite3_get_table( sqlite3 *db,255); line-height:1.5!important">char *zsql,255); line-height:1.5!important">char ***pazResult,0); line-height:1.5!important"> Results of the query int *pnRow,0); line-height:1.5!important"> Number of result rows written here int *pnColumn,0); line-height:1.5!important"> Number of result columns written here char **pzErrmsg */ ); void sqlite3_free_table(char **result);

第1个参数不再多说,看前面的例子。
第2个参数是sql 语句,跟sqlite3_exec 里的sql 是一样的。是一个很普通的以\0结尾的char*字符串。
第3个参数是查询结果,它依然一维数组(不要以为是二维数组,更不要以为是三维数组)。它内存布局是:字段名称,后面是紧接着是每个字段的值。下面用例子来说事。
第4个参数是查询出多少条记录(即查出多少行,不包括字段名那行)。
第5个参数是多少个字段(多少列)。
第6个参数是错误信息,跟前面一样,这里不多说了。

pazResult返回的字符串数量实际上是(*pnRow+1)*(*pnColumn),因为前(*pnColumn)个是字段名

修改上篇的例子,使用sqlite3_get_table,来去的结果集:

nResult = sqlite3_exec(db,&errmsg); char** pResult; int nRow; int nCol; nResult = sqlite3_get_table(db,&pResult,&nRow,&nCol,128); line-height:1.5!important">0; } string strOut; int nIndex = nCol; 0;i<nRow;i++) { int j=0;j<nCol;j++) { strOut+=pResult[j]; strOut+="; strOut+=pResult[nIndex]; strOut+="; ++nIndex; } } sqlite3_free_table(pResult); cout<<strOut<<endl; sqlite3_close(db); 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; }*/

修改董淳光

猜你在找的Sqlite相关文章