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. Ajax详解

    一:什么是Ajax AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法 ...

  2. HDOJ 4770 Lights Against Dudely

    状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. 为什么Java不适合游戏开发

    Strawberry Cow Bear: why java sucks for game developmenthttp://strawberrycowbear.blogspot.jp/2011/02 ...

  4. iOS9,导航控制器中的子控制器设置StatusBar状态失效的问题

    iOS9之前控制StatusBar的两种方式: 第一种方式:全局控制StatusBar 1. 在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppe ...

  5. react+redux官方实例TODO从最简单的入门(3)-- 删

    上一篇文章我们实现了增删改查中<增>这个功能 那么这一篇我们将实现第二个功能,删! 首先增加一个状态: actions中增加对应的约定 到reducer里面设置执行的函数(这里todo.i ...

  6. Typescript基础类型

    1.布尔值__boolean 2.数字__number----除了支持十进制和十六进制字面量,Typescript还支持ECMAScript 2015中引入的二进制和八进制字面量. 3.字符串__st ...

  7. Andrew Ng在coursera上的ML课程_知识点笔记_(1)

    1.Feature Scaling(特征缩放): 如上图所示,x1是房屋面积,x2是房间个数,若不进行特征缩放,则代价函数J的曲线近似为一个瘦长的椭圆(我暂时这么理解,θ1和θ2分别是x1和x2的权值 ...

  8. java---构造器

    public class SomeTrying{ public static void main(String[] args){ new Son(); new Son().Father(); } } ...

  9. Markdown基本语法

    Markdown 基本语法记录 # 欢迎使用 Cmd Markdown 编辑阅读器 ------ 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,**Cmd M ...

  10. jquery的checkbox,radio,select等方法总结

    jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻.时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对 ...