标签:
FMDB 是基于 SQLite 封装的 面向对对象(OC) 的API.
FMDB 需要libsqlite3.0 系统库的支持
///////////////////////// 实现代码 ////////////////////////////////////////////////
#import "PPFMDBTool.h"
#import "PPShop.h"
#import "FMDB.h"
@implementation PPFMDBTool
SingletonM(fmdbTool)
static FMDatabase *_db;
+ (void)initialize
{
// 1. 打开数据库
_db = [FMDatabase databaseWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shop.sqilte"]];
BOOL isSuc = [_db open];
if (isSuc) {
// 2. 创建表
[_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"];
}else{
LogGreen(@"打开数据库 -- 失败");
}
}
+ (void)insertShop:(PPShop *)shop
{
[_db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %f);", shop.name, shop.price];
}
+ (NSArray *)shops
{
// 查询 - 得到结果集
FMResultSet *set = [_db executeQuery:@"SELECT * FROM t_shop;"];
NSMutableArray *shops = [NSMutableArray array];
// 不断往下取数据
while ([set next]) {
// 获得当前所指向的数据
PPShop *shop = [[PPShop alloc] init];
shop.name = [set stringForColumn:@"name"];
shop.price = [set doubleForColumn:@"price"];
[shops addObject:shop];
}
return shops;
}
+ (void)deleteShop{
// 删除 价格低于500 的记录
[_db executeUpdate:@"DELETE FROM t_shop WHERE price < 5000;"];
}
标签:
原文地址:http://www.cnblogs.com/guangleijia/p/4846617.html