iOS---基于对Sqlilte3的二次包装的第三次包装--->JKDBModel ,一个好用的离线缓存库
https://github.com/Joker-King/JKDBModel
1.将FMDB和DBModel拖入项目中,然后添加libsqlite3.dylib
#import "JKDBModel.h"
@interface WrksheetInfoModel : JKDBModel
|
3./** 数据库中是否存在表 */
+ (BOOL)isExistInTable;
/** 保存或更新 * 如果不存在主键,保存, * 有主键,则更新 */
- (BOOL)saveOrUpdate;
/** 保存单个数据 */
- (BOOL)save;
/** 批量保存数据 */
+ (BOOL)saveObjects:(NSArray *)array;
/** 更新单个数据 */
- (BOOL)update;
/** 批量更新数据*/
+ (BOOL)updateObjects:(NSArray *)array;
/** 删除单个数据 */
- (BOOL)deleteObject;
/** 批量删除数据 */
+ (BOOL)deleteObjects:(NSArray *)array;
/** 通过条件删除数据 */
+ (BOOL)deleteObjectsByCriteria:(NSString *)criteria;
/** 清空表 */
+ (BOOL)clearTable;/** 查询全部数据 */+ (NSArray *)findAll;
/** 通过主键查询 */
+ (instancetype)findByPK:(int)inPk;
/** 查找某条数据 */
+ (instancetype)findFirstByCriteria:(NSString *)criteria;
/** 通过条件查找数据 * 这样可以进行分页查询 @" WHERE pk > 5 limit 10" */
+ (NSArray *)findByCriteria:(NSString *)criteria;
/** * 创建表 * 如果已经创建,返回YES */
+ (BOOL)createTable;#pragma mark - must be override method
/** 如果子类中有一些property不需要创建数据库字段,那么这个方法必须在子类中重写 */
+ (NSArray *)transients;
4.使用示例
#pragma mark - 插入数据
/** 创建多条子线程 */ - (IBAction)insertData:(id)sender { for (int i = 0; i < 1; i++) { User *user = [[User alloc] init]; user.name = [NSString stringWithFormat:@"麻子%d",i]; user.sex = @"男"; user.age = 10+i; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [user save]; }); } } /** 子线程一:插入多条用户数据 */ - (IBAction)insertData2:(id)sender { dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL); dispatch_async(q1, ^{ for (int i = 0; i < 5; ++i) { User *user = [[User alloc] init]; user.name = @"赵五"; user.sex = @"女"; user.age = i+5; [user save]; } }); } - (IBAction)insertData3:(id)sender { for (int i = 0; i < 1000; ++i) { User *user = [[User alloc] init]; user.name = @"张三"; user.sex = @"男"; user.age = i+5; [user save]; } } /** 子线程三:事务插入数据 */ - (IBAction)insertData4:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < 500; i++) { User *user = [[User alloc] init]; user.name = [NSString stringWithFormat:@"李四%d",i]; user.age = 10+i; user.sex = @"女"; [array addObject:user]; } [User saveObjects:array]; }); } #pragma mark - 删除数据 /** 通过条件删除数据 */ - (IBAction)deleteData:(id)sender { // [User deleteObjectsByCriteria:@" WHERE pk < 10"]; [User deleteObjectsWithFormat:@"Where %@ < %d",@"pk",10]; } /** 创建多个线程删除数据 */ - (IBAction)deleteData2:(id)sender { for (int i = 0; i < 5; i++) { User *user = [[User alloc] init]; user.pk = 1+i; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [user deleteObject]; }); } } /** 子线程用事务删除数据 */ - (IBAction)deleteData3:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < 500; i++) { User *user = [[User alloc] init]; user.pk = 501+i; [array addObject:user]; } [User deleteObjects:array]; }); } #pragma mark - 修改数据 /** 创建多个线程更新数据 */ - (IBAction)updateData1:(id)sender { for (int i = 0; i < 5; i++) { User *user = [[User alloc] init]; user.name = [NSString stringWithFormat:@"更新%d",i]; user.age = 120+i; user.pk = 5+i; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [user update]; }); } } /**单个子线程批量更新数据,利用事务 */ - (IBAction)updateData:(id)sender { dispatch_queue_t q3 = dispatch_queue_create("queue3", NULL); dispatch_async(q3, ^{ NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < 500; i++) { User *user = [[User alloc] init]; user.name = [NSString stringWithFormat:@"啊我哦%d",i]; user.age = 88+i; user.pk = 10+i; [array addObject:user]; } [User updateObjects:array]; }); } #pragma mark - 查询 /** 查询单条记录 */ - (IBAction)queryData1:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"第一条:%@",[User findFirstByCriteria:@" WHERE age = 20 "]); }); } /** 条件查询多条记录 */ - (IBAction)queryData2:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"小于20岁:%@",[User findByCriteria:@" WHERE age < 20 "]); }); } /** 查询全部数据 */ - (IBAction)queryData3:(id)sender { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"全部:%@",[User findAll]); }); } /** 分页查询数据 */ - (IBAction)queryData:(id)sender { static int pk = 5; NSArray *array = [User findByCriteria:[NSString stringWithFormat:@" WHERE pk > %d limit 10",pk]]; pk = ((User *)[array lastObject]).pk; NSLog(@"array:%@",array); } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSString *title = @"查询"; int type = 3; if ([segue.identifier isEqualToString:@"One"]) { title = @"查询一条数据"; type = 1; } else if ([segue.identifier isEqualToString:@"Two"]){ title = @"条件查询"; type = 2; }else if ([segue.identifier isEqualToString:@"Three"]){ title = @"查询全部"; type = 3; }else if ([segue.identifier isEqualToString:@"Four"]){ title = @"分页查询"; type = 4; } QueryTableViewController *destVC = segue.destinationViewController; destVC.title = title; destVC.type = type; } |
1.模型属性全部用NSString修饰
2.模型属性赋值时
model.orderHisInfo = [[NSString alloc] initWithData:[UtilitytoJSONData:dic[@"orderHisInfo”]] encoding:NSUTF8StringEncoding]; //查看进程
3.取值时,取出来是数组或者字典
[Utility toArrayOrNSDictionary:[self.model.orderHisInfo dataUsingEncoding:NSUTF8StringEncoding]];
|
// 将JSON串转化为字典或者数组 数组转换字符串 + (id)toArrayOrNSDictionary:(NSData *)jsonData{ NSError *error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; if (jsonObject != nil && error == nil){ return jsonObject; }else{ // 解析错误 return nil; } } // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData{ NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:&error]; NSLog(@"%@", jsonData); if ([jsonData length] != 0 && error == nil){ return jsonData; }else{ return nil; } }
|