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. Lamda表达式使用

    public class Lambda { public static void main(String[] args) { Lambda lambda=new Lambda(); String so ...

  2. iOS9网络适配(ATS)

    [转]iOS9 new_001:iOS9网络适配(ATS) 下载Xcode7打开APP后大家都发现自己的APP无法联网了,why? 苹果官方文档介绍如下: App Transport Security ...

  3. PHP代码 如何网页获取用户的openid

    public function getOpenid($appid, $appsecret) { $SERVER_NAME = $_SERVER['SERVER_NAME']; $REQUEST_URI ...

  4. 检查日期是否为节假日api

    http://www.easybots.cn/api/holiday.php?d=20160104 返回值: 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2: 检查一个日期是否 ...

  5. css兼容性大坑

    一. \:选择IE6+//区分 IE 8(不实用) .title{ color:yellow\0; color: red\9\0;} \9在 IE 6及其以上都可以识别(但是 IE11不识别 ,IE ...

  6. UITableViewCell 的附件类型 accessoryType 选中、详情、箭头

    UITableViewCell * cell = [[UITableViewCell alloc] init]; 设置cell的附件类型: // >1 打钩 选中//    cell.acces ...

  7. idea开发工具破解地址

    链接失效可以使用激活码.激活码不需要联网也可以开发. idea 在注册时选择 License server ,填http://idea.iteblog.com/key.php ,然后点击激活. 激活码 ...

  8. shell 脚本中的运算

    #!/bin/bash read -p "please Insert two integer values: " a b if [ ! -n "$a" -o ! ...

  9. 前后端分离中,Gulp实现头尾等公共页面的复用

    前言 通常我们所做的一些页面,我们可以从设计图里面看出有一些地方是相同的.例如:头部,底部,侧边栏等等.如果前后端分离时,制作静态页面的同学,对于这些重复的部分只能够通过复制粘贴到新的页面来,如果页面 ...

  10. 如何在eclipse中通过Juit进行单元测试

    1.什么是Junit Junit即单元测试,是JAVA语言的单元测试框架,是对程序的一个方法所进行的测试 一般都是由程序员自己通过Junit来进行测试,因此单元测试也叫程序员测试: 如果测试人员熟悉程 ...