很多第三方,是优秀的,使用起来是方便快捷的,但是总有少部分人是抵制的。抵制原因很简单,只会用别人的,自己写写不出来。宝宝不想说话。不过笔者也建议各位经常封装点自己的东西,哪怕是基于第三方的,没关系,既然不明白第三方的原理,就不需要知道,就像咱们不知道为什么写一个Button,他就可以这样用就行,咱们只需要知道自己是怎么封装的就行。
这是笔者第一次用FMDB,之前说实话,数据存储大批量的存储,做的不多,即使做也是用Coredata。
笔者为什么要封装FMDB呢,也是源于Coredata给的灵感,或者说用Coredata用习惯了,不想直接写SQL语句,所以把SQL语句封装了一下,同时,对数据存储和解析过程也进行了一次封装,舍弃网上很多教程中的模型存储,为什么要舍弃这种方法呢?封装嘛,就是为了通用,用模型,就有些限制了,所以舍弃了。
一:最终封装的方法的使用
1,单例创建一个表,并设置表的字段和字段类型
[[ZYFMDBManager shareInstance] creatTableWithName:@"test" Parmas:@{@"age":@"int",@"name":@"text",@"id":@"int"}];
2,单例调用删除数据的方法,传入表名,和删除条件,这个条件,传入格式如下,不需要传入sql语句
[[ZYFMDBManager shareInstance] deleDataWithtable:@"test" deleStr:@"age<30"];
3,单例调用增加数据的方法,传入表名,和增加的数据,这里传入的是字典,暂只支持一个个数据添加。
[[ZYFMDBManager shareInstance] addDataDic:@{@"age":@25,@"name":@"zhang",@"id":@12} toTable:@"test"];
4,单例查询数据,传入表名和表中的字段名,字段名是放在字典中的key,其value无作用。
NSArray *arr = [[ZYFMDBManager shareInstance] queryDatWithTable:@"test" queryStr:@"age<30" dic:@{@"age":@"",@"name":@"",@"id":@""}];
NSLog(@"%@",arr);
注:扩展方法
第三个方法,插入数据,只支持单个插入,这里为了能高效快速的插入数据,笔者推荐一个GCD的用法
//创建一个并行线程队列
dispatch_queue_t queue_1 = dispatch_queue_create("queue_1", DISPATCH_QUEUE_CONCURRENT);
//创建一个串行队列
// dispatch_queue_t queue_2 = dispatch_queue_create("queue_2", DISPATCH_QUEUE_SERIAL);
//确定任务数量
size_t count = 10000;
//异步提交遍历数组
dispatch_apply(count, queue_1, ^(size_t i) {
// NSLog(@"%@",array[i]);
});
用户可以将插入方法写到异步提交里面,执行次数就是count。有不理解的,去看看GCD吧。笔者没有写更新的方法,个人认为,更改数据不如删了重新插入。
若需要封装出来的那个类,请来512847147找我要(ps打个广告不容易0.0)当然,下面代码也是全部代码了。
二:封装过程 (可不看)
对使用方法的方法的封装
1),开放出来的方法
//
// ZYFMDBManager.h
// FMDBDemo
//
// Created by ybon on 16/9/21.
// Copyright © 2016年 ybon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FMDB.h"
@interface ZYFMDBManager : NSObject
+ (instancetype)shareInstance;
#pragma mark 创建数据库 表名 字典里是字段类型和字段名
- (void)creatTableWithName:(NSString *)tableName Parmas:(NSDictionary *)dic;
#pragma mark 添加数据
- (void)addDataDic:(NSDictionary *)dic toTable:(NSString *)tableName;
#pragma mark 查找数据 dic为数据模型,只取key
- (NSArray *)queryDatWithTable:(NSString *)tableName queryStr:(NSString *)queryStr dic:(NSDictionary *)dic;
#pragma mark 删除数据
- (void)deleDataWithtable:(NSString *)tableName deleStr:(NSString *)deleStr;
@end
2),实现方法
#import "ZYFMDBManager.h"
#import "ZYSqlStringManager.h"
@implementation ZYFMDBManager
{
NSString *dbPath;
FMDatabase *dbBase;
}
+ (instancetype)shareInstance{
static ZYFMDBManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[ZYFMDBManager alloc] init];
});
return manager;
}
#pragma mark 创建数据库 表名 字典里是字段类型和字段名
- (void)creatTableWithName:(NSString *)tableName Parmas:(NSDictionary *)dic{
NSString *homePath = NSHomeDirectory();
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/ZYInstance.squlite",homePath];
//创建数据库
dbBase = [FMDatabase databaseWithPath:filePath];
//打开数据库
BOOL isOpen = [dbBase open];
if (isOpen == YES) {
NSLog(@"Open");
//创建表
NSString *creatSql = [ZYSqlStringManager getcreatTableStringWithName:tableName dic:dic];
if ([dbBase executeUpdate:creatSql]) {
NSLog(@"creat success");
}
}else{
NSLog(@"File");
}
}
#pragma mark 添加数据
- (void)addDataDic:(NSDictionary *)dic toTable:(NSString *)tableName{
NSArray *arr = dic.allValues;
// NSString *valueStr = [arr componentsJoinedByString:@","];
[ZYSqlStringManager insertDataWithName:tableName dic:dic Block:^(NSString *sqlStr) {
if ([dbBase executeUpdate:sqlStr values:arr error:nil]) {
NSLog(@"insert success");
}
}];
}
#pragma mark 查找数据 dic为数据模型,只取key
- (NSArray *)queryDatWithTable:(NSString *)tableName queryStr:(NSString *)queryStr dic:(NSDictionary *)dic{
FMResultSet *set = [dbBase executeQuery:[ZYSqlStringManager queryStringWithName:tableName queryStr:queryStr] withArgumentsInArray:nil];
NSMutableArray *arr = [[NSMutableArray alloc] init];
while (set.next) {
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc] init];
for (NSString *key in dic) {
NSString *str = [set stringForColumn:key];
[dataDic setValue:str forKey:key];
}
[arr addObject:dataDic];
}
return arr;
}
#pragma mark 删除数据
- (void)deleDataWithtable:(NSString *)tableName deleStr:(NSString *)deleStr{
BOOL isSuccess = [dbBase executeUpdate:[ZYSqlStringManager deleStringWithName:tableName deleStr:deleStr] withArgumentsInArray:nil];
if (isSuccess) {
NSLog(@"dele success");
}
}
@end
对sql语句的封装和拼接
1),开放出来的方法
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ZYSqlStringManager : NSObject
#pragma mark 创建表需要的字符串
+ (NSString *)getcreatTableStringWithName:(NSString *)name dic:(NSDictionary *)dic;
#pragma mark 添加数据的sql语句
+ (void)insertDataWithName:(NSString *)name dic:(NSDictionary *)dic Block:(void(^)(NSString *sqlStr))block;
#pragma mark 查找数据的sql语句
+ (NSString *)queryStringWithName:(NSString *)name queryStr:(NSString *)queryStr;
#pragma mark 删除数据的sql语句
+ (NSString *)deleStringWithName:(NSString *)name deleStr:(NSString *)deleStr;
@end
2),实现方法
#import "ZYSqlStringManager.h"
@implementation ZYSqlStringManager
#pragma mark 创建表需要的字符串
+ (NSString *)getcreatTableStringWithName:(NSString *)name dic:(NSDictionary *)dic{
NSString *sqlStr = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@)",name,[self parmasStringWithParmas:dic]];
return sqlStr;
}
#pragma mark 添加数据的sql语句
+ (void)insertDataWithName:(NSString *)name dic:(NSDictionary *)dic Block:(void(^)(NSString *sqlStr))block{
[self addDataStringWithDic:dic Block:^(NSString *keyStr, NSString *valueStr) {
NSString *sqlStr1 = [NSString stringWithFormat:@"insert into %@(%@) values (%@)",name,keyStr,valueStr];
block(sqlStr1);
}];
}
#pragma mark 查找数据的sql语句
+ (NSString *)queryStringWithName:(NSString *)name queryStr:(NSString *)queryStr{
NSString *query = [NSString stringWithFormat:@"select * from %@ where %@",name,queryStr];
return query;
}
#pragma mark 删除数据的sql语句
+ (NSString *)deleStringWithName:(NSString *)name deleStr:(NSString *)deleStr{
NSString *del = [NSString stringWithFormat:@"delete from %@ where %@",name,deleStr];
return del;
}
#pragma mark 拼接创表时需要的字符串
+ (NSString *)parmasStringWithParmas:(NSDictionary *)parmas{
NSMutableString *parmasString = [[NSMutableString alloc] init];
for (NSString *key in parmas) {
[parmasString appendFormat:@"%@ %@",key,[parmas objectForKey:key]];
if (key != [[parmas allKeys]lastObject]) {
[parmasString appendFormat:@","];
}
}
return parmasString;
}
#pragma mark 拼接添加数据需要的字符串
+ (void)addDataStringWithDic:(NSDictionary *)parmas Block:(void(^)(NSString *keyStr,NSString *valueStr))block{
NSArray *allKeys = parmas.allKeys;
NSMutableString *parmasString = [[NSMutableString alloc] init];
NSString *keyString = [allKeys componentsJoinedByString:@","];
for (NSString *key in parmas) {
[parmasString appendFormat:@"%@",@"?"];
if (key != [[parmas allKeys]lastObject]) {
[parmasString appendFormat:@","];
}
}
block(keyString,parmasString);
}
@end