1.创建可修改的数据库文件

//应用包内的内容是不可写的,所以需要把应用包内的数据库拷贝一个副本到资源路径去
- (void)createEditableDatabase{ BOOL success;
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths objectAtIndex:];
NSString *writableDB = [documentDir stringByAppendingPathComponent:@"catalog.db"]; success = [manager fileExistsAtPath:writableDB]; if (success) {
NSLog(@"已经存在");
return;
} NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"catalog.db"];
success = [manager copyItemAtPath:defaultPath toPath:writableDB error:&error];
if (!success) {
NSAssert1(, @"Failed to create writable database file:'%@'.", [error localizedDescription]);
}else NSLog(@"成功写入");
}

2.初始化数据库

- (void)initDatabase{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"catalog" ofType:@"db"];
//NSLog(@"bundle = %@\npath = %@",[NSBundle mainBundle],path);
if (sqlite3_open([path UTF8String],&database) == SQLITE_OK) {
NSLog(@"Opening Database");
}else{
sqlite3_close(database);
NSAssert1(, @"Failed to open database:'%s'.", sqlite3_errmsg(database));
}
}

3.查询

//execute sql statement
- (NSMutableArray *)getAllProducts{ NSMutableArray *products = [NSMutableArray new];
// const char *sql = "SELECT product.ID,product.Name,Manufacturer.name,product.details,product.price,product.quantityonhand,country.country,product.image FROM Product,Manufacturer,Country WHERE manufacturer.manufacturerID = product.manufacturerID and product.countryoforiginID = ?";
//配合sqlite3_bind_int(stmt,1,2)可使用参数化sql语句查询 const char *sql = "SELECT product.ID,product.Name,Manufacturer.name,product.details,product.price,product.quantityonhand,country.country,product.image FROM Product,Manufacturer,Country WHERE manufacturer.manufacturerID = product.manufacturerID and product.countryoforiginID = country.countryID"; NSLog(@"\nsql = %s",sql); sqlite3_stmt *stmt;
int sqlResult = sqlite3_prepare_v2(database,sql,-,&stmt,NULL);
//sqlite3_bind_int(stmt,1,2);//sql语句参数查询,语句、参数索引、参数值 if (sqlResult == SQLITE_OK) {
NSLog(@"Ready to print sth");
int time = ;
while (sqlite3_step(stmt) == SQLITE_ROW) { Product *product = [Product new];
char *name = (char*)sqlite3_column_text(stmt,);
char *manufacturer = (char*)sqlite3_column_text(stmt,);
char *details = (char*)sqlite3_column_text(stmt, );
char *countryOfOrigin = (char*)sqlite3_column_text(stmt, );
char *image = (char*)sqlite3_column_text(stmt, ); NSLog(@"%d,name = %s \n",time++,name); product.ID = sqlite3_column_int(stmt,);
product.name = (name)?[NSString stringWithUTF8String:name]:@"";
product.manufacturer = (manufacturer)?[NSString stringWithUTF8String:manufacturer]:@"";
product.details = (details)?[NSString stringWithUTF8String:details]:@"";
product.price = sqlite3_column_double(stmt,);
product.quantity = sqlite3_column_int(stmt,);
product.countryOfOrigin = (countryOfOrigin)?[NSString stringWithUTF8String:countryOfOrigin]:@"";
product.image = (image)?[NSString stringWithUTF8String:image]:@""; [products addObject:product];
} sqlite3_finalize(stmt);
}else{
NSLog(@"read failed:%d",sqlResult);
} return products;
}

4.关闭数据库

- (void)closeDatabase{
if (sqlite3_close(database) != SQLITE_OK) {
NSAssert1(, @"Error:failed to close database:'%s'.", sqlite3_errmsg(database));
}
}

iOS SQLite3的使用的更多相关文章

  1. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  2. iOS sqlite3数据库解析

    看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...

  3. iOS Sqlite3 Demo 及 FMDB Demo

    本文是主要实现了三个函数: testSQLite3 是测试系统自带的sqlite3的demo testFMDB是测试FMDB存取简单的数据类型的 的demo testFMDB2是将任意对象作为一个整体 ...

  4. ios sqlite3的简单使用

    第一:创建表格 //创建表格 -(void)creatTab{ NSString*creatSQL=@"CREATE TABLE IF NOT EXISTS PERSIONFO(ID INT ...

  5. iOS——sqlite3的使用(iOS嵌入式关系数据库)

    1>添加sqlite3动态库:libsqlite3.dylib,CoreGraphics.framework,UIKit.framework,Foundation.framework 2> ...

  6. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  7. iOS数据持久化-OC

    沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...

  8. iOS 数据存储之SQLite3的使用

    SQLite3是iOS内嵌的数据库,SQLite3在存储和检索大量数据方面非常有效,它使得不必将每个对象都加到内存中.还能够对数据进行负责的聚合,与使用对象执行这些操作相比,获得结果的速度更快. SQ ...

  9. IOS 使用wxsqlite3为sqlite3数据库加密

    1,下载wxsqlite3 地址http://jaist.dl.sourceforge.net/project/wxcode/Components/wxSQLite3/wxsqlite3-3.1.1. ...

随机推荐

  1. VB操作EXCEL文件

    用VB操作Excel(VB6.0)(整理) 首先创建Excel对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel. ...

  2. centos 7配置网络 更新yum源

    cd /etc/sysconfig/network-script/ 找到对应的ifcfg-entxxxx文件,然后添加网关,修改dhcp为static,静态ip,添加IPADDR ip地址.onboo ...

  3. <hr>标签不止创建html水平线也可以画圆噢

    看到上面这张图,第一反应是用photoshop类似作图软件画出来的,但那是华丽丽的错觉.一个简单的<hr>标签就能完成漂亮的效果,而不仅仅是创建html水平线.想知道怎么实现吗?Let's ...

  4. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...

  5. git知识点整理

  6. 【sdoi2013】森林 BZOJ 3123

    Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负整数 ...

  7. ThinkInside

    #sideBar,#blog_post_info_block { display: none;}#under_post_news { display: none;} /*评论框大小*/ #tbComm ...

  8. erlang 虚机crash

    现网服务,每次更新一个服务时,另外一个集群所有node 都跟着同时重启一遍,这么调皮,这是闹哪样啊.. 看系统日志:/var/log/messages Oct 30 15:19:41 localhos ...

  9. repo 修改邮箱地址

    需要重新运行 repo init 被带上参数: --config-name xx@a.com

  10. go:channel(未完)

    注:1)以下的所有讨论建立在包含整形元素的通道类型之上,即 chan int 2)对于“<-”我的理解是,它可能是一个操作符(接收操作符),也  可能是类型的一部分(如“chan<- in ...