.h文件
/* 类方法,快速实例化对象/
+ (instancetype)shareDBManager;
/* 增加模型数据/
- (BOOL)insertDbWithModel:(id)model;
/* 删除模型数据/
- (BOOL)deleteDbWithModel:(id)model;
/* 修改模型数据/
- (BOOL)updateDbWithNewModel:(id)newModel oldModel:(id)oldModel;
/* 查询整个表单数据/
- (NSMutableArray )selectWithModel:(id)model whereWithUlrStr:(NSString )urlStr;
//- (NSMutableArray *)selectWithModel:(id)model;
/* 按条件查询数据/
- (BOOL)selectWithModel:(id)model whereWithDict:(NSDictionary *)dict;
/* 删除所有数据/
- (BOOL)removeAllDataWithModel:(id)model;
/* 条件删除/
- (BOOL)removeAllDataWithModel:(id)model whereWithUlrStr:(NSString *)urlStr;
.m文件
(instancetype)shareDBManager{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[WinBoDBManager alloc] init];
});return manager;
}(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [super allocWithZone:zone];
});
return manager;
}
pragma mark - 重写父类init,打开数据库
(instancetype)init{
if (self = [super init]) {
// 打开数据库
NSString *path = [NSString stringWithFormat:@”%@/Documents/zyzDb.db”,NSHomeDirectory()];
_queue = [[FMDatabaseQueue alloc] initWithPath:path];
DLog(@”path:%@”,path);
}return self;
}
pragma mark - 获取成员变量名
(NSArray )getPropertyListWithTableName:(NSString )tableName{
// 这个tableName就是模型的类名
NSMutableArray *propertyArray = [NSMutableArray array];// 成员变量的个数
unsigned int count;
Ivar *vars = class_copyIvarList(NSClassFromString(tableName), &count);for (int i=0; i
pragma mark - 创建表单
(void)createTableWithName:(NSString *)tableName{
// create table 表名(字段名,字段名···)
/**
创建表单名就是类名,可以通过类名,获取对应的字段名,通过tableName获取对应模型的类名
*/
// 获取一个类的成员变量名以及成员类型(模型类)
NSArray *propertyArray = [self getPropertyListWithTableName:tableName];// 数据库语句的拼接
NSMutableString *mString = [NSMutableString string];
for (int i=0; i
pragma mark - 插入数据
(BOOL)insertDbWithModel:(id)model{
// 注意这个方法是通过类,获取类名
NSString *tableName = [NSString stringWithUTF8String:object_getClassName(model)];
NSArray *propertyArray = [self getPropertyListWithTableName:tableName];[self createTableWithName:tableName];
// insert into 表 values(?,?)
NSMutableString *tempString = [NSMutableString string];
NSMutableArray *valueArray = [NSMutableArray array];
for (int i=0; i
pragma mark - 删除数据
(BOOL)deleteDbWithModel:(id)model{
// delete from 表名 where 字段
NSString *tableName = [NSString stringWithUTF8String:object_getClassName(model)];
NSArray *propertyArray = [self getPropertyListWithTableName:tableName];NSMutableString *keyString = [NSMutableString string];
NSMutableArray *valueArray = [NSMutableArray array];
for (int i=0; i
pragma mark - 更新
(BOOL)updateDbWithNewModel:(id)newModel oldModel:(id)oldModel{
NSString *tableName = [NSString stringWithUTF8String:object_getClassName(newModel)];
NSArray *propertyArray = [self getPropertyListWithTableName:tableName];// update 表名 set 字段名=值 where 字段名=值(条件)
NSMutableString *tempString1 = [NSMutableString string];
NSMutableString *tempString2 = [NSMutableString string];
// 存储新值
NSMutableArray *valueArray1 = [NSMutableArray array];
// 存储旧值
NSMutableArray *valueArray2 = [NSMutableArray array];
for (int i=0; i
pragma mark - 搜索
// 直接返回模型数据
//- (NSMutableArray *)selectWithModel:(id)model{
- (NSMutableArray )selectWithModel:(id)model whereWithUlrStr:(NSString )urlStr{
NSMutableArray *selectArray = [NSMutableArray array];
NSString *tableName = [NSString stringWithUTF8String:object_getClassName(model)];
NSString *sql;
if (urlStr) {
sql = [NSString stringWithFormat:@"select * from %@ where _urlStr='%@'",tableName,urlStr];
}else{
sql = [NSString stringWithFormat:@"select * from %@",tableName];
}
DLog(@"--%@",sql);
NSArray *propertyArray = [self getPropertyListWithTableName:tableName];
[_queue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:sql];
while ([result next]) {
// 表名即类名
// 通过类名获取该类,即实例化模型
Class class = NSClassFromString(tableName);
id model = [[class alloc] init];
// 注意,搜索出的是完整数据,之后遍历操作将模型赋值
for (int i=0; i<propertyArray.count; i++) {
[model setValue:[result stringForColumn:propertyArray[i]] forKey:propertyArray[i]];
}
// 添加到数组中
[selectArray addObject:model];
}
}];
return selectArray;
}
pragma mark - 条件查询
(BOOL)selectWithModel:(id)model whereWithDict:(NSDictionary *)dict{
__block BOOL isSucceed = NO;NSString *tableName = [NSString stringWithUTF8String:object_getClassName(model)];
NSString *sql;
for (NSString *key in dict) {
sql = [NSString stringWithFormat:@”select * from %@ where _%@=’%@’”,tableName,key,dict[key]];[_queue inDatabase:^(FMDatabase *db) { FMResultSet *result = [db executeQuery:sql]; while ([result next]) { // 表示有检索到数据 isSucceed = YES; } }];
}
return isSucceed;
}