导入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用法
原型:
- 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中可能是:
- NSArray * documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString * dbPath = [[documentPath objectAtIndex:0] stringByAppendingPathComponent:@“test.db”];
关闭数据库链接sqlite3_close用法
原型:
- int sqlite3_close(sqlite3 *ppDb);
ppDb为刚才使用sqlite3_open打开的数据库链接
执行sql操作sqlite3_exec用法
原型:
- 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>
- );
说明:通常,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是对应的,表示这个字段的字段名称
实例:
- 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 }