iOS SQLite3的使用
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的使用的更多相关文章
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- iOS sqlite3数据库解析
看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...
- iOS Sqlite3 Demo 及 FMDB Demo
本文是主要实现了三个函数: testSQLite3 是测试系统自带的sqlite3的demo testFMDB是测试FMDB存取简单的数据类型的 的demo testFMDB2是将任意对象作为一个整体 ...
- ios sqlite3的简单使用
第一:创建表格 //创建表格 -(void)creatTab{ NSString*creatSQL=@"CREATE TABLE IF NOT EXISTS PERSIONFO(ID INT ...
- iOS——sqlite3的使用(iOS嵌入式关系数据库)
1>添加sqlite3动态库:libsqlite3.dylib,CoreGraphics.framework,UIKit.framework,Foundation.framework 2> ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- iOS 数据存储之SQLite3的使用
SQLite3是iOS内嵌的数据库,SQLite3在存储和检索大量数据方面非常有效,它使得不必将每个对象都加到内存中.还能够对数据进行负责的聚合,与使用对象执行这些操作相比,获得结果的速度更快. SQ ...
- IOS 使用wxsqlite3为sqlite3数据库加密
1,下载wxsqlite3 地址http://jaist.dl.sourceforge.net/project/wxcode/Components/wxSQLite3/wxsqlite3-3.1.1. ...
随机推荐
- href="javascript:;" 作用
<a href="javascript:;" onclick="doExport(this)" class="easyui-linkbutton ...
- 【bzoj1700】Problem Solving 解题
题目描述 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地说,他们有P (1 <= P <= 300) 道题目要做. 他们还离开了农场并且象普通人一 ...
- Windows下将nginx安装为服务运行
今天看到nginx这个小服务器软件正式版更新到了1.4.2,想玩下它.这个服务器软件虽小,但功能强大,是开源软件,有着良好的性能,被很多个人.企业,甚至大型企业所使用! 由于是在Windows下,所以 ...
- int与CString互相转化
int num; CString str; //int转CString num=; str.Format(_T("%d"),num); //CString转int str=L&qu ...
- Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...
- json格式化工具
1.JsonViewer 可对json数据进行查看.格式化.编辑...... 2.在线工具 http://json.parser.online.fr/
- ng-repeat里创建的自定义指令
在ng里,所有的指令在按照意愿正常工作之前的都需要编译一下,包含angularJS的自定义指令. ng模板里的所有指令都会在angularJS加载完毕之后编译一下,所以那些自定义指令和事件才能工作. ...
- python之路十五
CSS position 属性 定义和用法position 属性规定元素的定位类型.说明这个属性定义建立元素布局所用的定位机制.任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身 ...
- Visual Studio 常用快捷键备忘
在代码中插入书签 用途 操作 vs2013 快速在自定义的不同代码位置跳转 首先点击: 编辑=>书签=>启用书签 然后再在代码编辑窗口 ctrl+k, k (取消书签,再按一次 ctr ...
- c#获取外网IP地址的方法
1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...