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. ...
随机推荐
- FFT NNT
算算劳资已经多久没学新算法了,又要重新开始学辣.直接扔板子,跑...话说FFT算法导论里讲的真不错,去看下就懂了. //FFT#include <cstdio> #include < ...
- 第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash
1. UIImage imageWithContentsOfFile卡顿 [[UIImage alloc] initWithContentsOfFile 卡顿 2.uitableview scroll ...
- H5案例分享:JS手势框架 —— Hammer.js
JS手势框架 -- Hammer.js 一.hammer.js简介 hammerJS是一个开源的,轻量级的触屏设备javascript手势库,它可以在不需要依赖其他东西的情况下识别触摸,鼠标事件.允许 ...
- web 安全杂谈
以前写过一篇关于session.cookie的博文,都是简单的介绍.不过session和cookie和网络安全可有着密切的关系. 今天主要从这几个方面总结下最近学到的东西: 1. session 两种 ...
- css text-overflow:ellipsis 文字多余剪切
text-overflow: ellipsis;多度剪切white-space: nowrap;禁止换行overflow: hidden;多余隐藏
- MongoDB安装
安装 1>设置MongoDB目录 cd /home/apps 附:centOS下创建目录命令 mkdir /home/apps 2>下载mongodb curl -O http ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 一对一还是一对多? MVP设计前提
设计MVP之前,先要确定好以下模块之间是一对多还是一对一的关系: View ---> Presenter --> Model --> Interface(URL) 就是一个View只 ...
- 微软KinectV2深度传感器在Ubuntu上的配置和使用
最新博客地址已转到: http://blog.csdn.net/zzlyw?viewmode=contents ------------------------------------------ ...
- 一堆LCT板子
搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...