FMDB的简单封装
来源:互联网 发布:mac装系统开机按哪个键 编辑:程序博客网 时间:2024/05/21 05:57
@interface Manager : NSObject
@property (nonatomic,strong) FMDatabaseQueue *dataQueue;
+(Manager*)sharedManager;
-(void)initQueue;
-(BOOL)insertToDB:(NSString *)table dic:(NSDictionary *)where;
-(void)insertToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(BOOL b))block;
-(BOOL)deleteToDB:(NSString *)table dic:(NSDictionary *)where;
-(void)deleteToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(BOOL b))block;
-(BOOL)updateToDB:(NSString *)table dic:(NSDictionary *)dic where:(NSDictionary *)where;
-(void)updateToDB:(NSString *)table dic:(NSDictionary *)dic where:(NSDictionary *)where
callback:(void(^)(BOOL b))block;
-(NSDictionary *)searchToDB:(NSString *)sql;
-(NSArray *)searchToDBs:(NSString *)sql;
-(NSDictionary *)searchToDB:(NSString *)table dic:(NSDictionary *)where;
-(void)searchToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(NSDictionary *dic))result;
@end
@implementation Manager
static Manager *shareDataBase =nil;
static FMDatabaseQueue *queue =nil;
+(Manager *)sharedManager{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareDataBase = [[selfalloc] init];
[[selfalloc] initQueue];
});
returnshareDataBase;
}
-(void)initQueue{
queue = [[FMDatabaseQueuealloc] initWithPath:数据库的路径字符串];
}
-(FMDatabaseQueue *)dataQueue{
if (_dataQueue !=queue) {
_dataQueue =queue;
}
return_dataQueue;
}
-(BOOL)insertToDB:(NSString *)table dic:(NSDictionary *)where{
__blockBOOL result = YES;
[selfinsertToDB:table dic:where callback:^(BOOL b) {
result = b;
}];
return result;
}
-(void)insertToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(BOOL b))block{
[queueinDatabase:^(FMDatabase *db) {
NSMutableString* insertKeysString = [NSMutableStringstring];
NSMutableString* insertValuesString = [NSMutableStringstring];
NSMutableArray* values = [NSMutableArrayarrayWithCapacity:0];
NSArray* keys = where.allKeys;
for (int i=0; i< keys.count;i++){
NSString* key = [keysobjectAtIndex:i];
[insertKeysString appendFormat:@"%@,",key];
[values addObject:[whereobjectForKey:key]];
[insertValuesString appendString:@"?,"];
}
[insertKeysString deleteCharactersInRange:NSMakeRange(insertKeysString.length - 1,1)];
[insertValuesString deleteCharactersInRange:NSMakeRange(insertValuesString.length - 1,1)];
NSString* insertSQL = [NSStringstringWithFormat:@"INSERT INTO %@ (%@) values(%@)",
table,insertKeysString,insertValuesString];
BOOL result = [dbexecuteUpdate:insertSQL withArgumentsInArray:values];
if(block !=nil)
block(result);
}];
}
-(BOOL)deleteToDB:(NSString *)table dic:(NSDictionary *)where{
__blockBOOL result = YES;
[selfdeleteToDB:table dic:where callback:^(BOOL b) {
result = b;
}];
return result;
}
-(void)deleteToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(BOOL b))block{
[queueinDatabase:^(FMDatabase *db) {
NSMutableArray* values = [NSMutableArrayarray];
NSString* wherekey = [selfdictionaryToSqlWhere:whereandValues:values isWhere:YES];
NSString* delete = [NSStringstringWithFormat:@"DELETE FROM %@ where %@ ",table,wherekey];
BOOL result = [dbexecuteUpdate:delete withArgumentsInArray:values];
if(block !=nil)
block(result);
}];
}
-(BOOL)updateToDB:(NSString *)table dic:(NSDictionary *)dic where:(NSDictionary *)where{
__blockBOOL result = YES;
[selfupdateToDB:table dic:dic where:wherecallback:^(BOOL b) {
result = b;
}];
return result;
}
-(void)updateToDB:(NSString *)table dic:(NSDictionary *)dic where:(NSDictionary *)where
callback:(void(^)(BOOL b))block{
[queueinDatabase:^(FMDatabase *db) {
NSMutableString* query = [NSMutableStringstringWithFormat:@"UPDATE %@ SET ",table];
NSMutableArray* values = [NSMutableArrayarrayWithCapacity:0];
if(dic !=nil&& dic.count>0){
NSString* wherekey = [selfdictionaryToSqlWhere:dicandValues:values isWhere:NO];
[query appendFormat:@" %@",wherekey];
wherekey = [selfdictionaryToSqlWhere:whereandValues:values isWhere:NO];
[query appendFormat:@" WHERE %@ ",wherekey];
}
BOOL result = [dbexecuteUpdate:query withArgumentsInArray:values];
if(block !=nil)
block(result);
}];
}
-(NSArray *)searchToDBs:(NSString *)sql{
__blockNSMutableArray *array = [NSMutableArrayarray];
[queueinDatabase:^(FMDatabase *db) {
FMResultSet* rs =[dbexecuteQuery:sql];
int num_cols = [rscolumnCount];
while ([rsnext]){
NSMutableDictionary *dict = [NSMutableDictionarydictionaryWithCapacity:num_cols];
for (int i =0; i < num_cols; i++) {
NSString * columnName = [rscolumnNameForIndex:i];
id columnValue = [rsobjectForColumnIndex:i];
[dict setObject:columnValueforKey:columnName];
}
[array addObject:dict];
}
}];
return array;
}
-(NSDictionary *)searchToDB:(NSString *)sql{
__blockNSDictionary *result = nil;
[queueinDatabase:^(FMDatabase *db) {
FMResultSet* rs =[dbexecuteQuery:sql];
int num_cols = [rscolumnCount];
NSMutableDictionary *dict = [NSMutableDictionarydictionaryWithCapacity:num_cols];
while ([rsnext]){
for (int i =0; i < num_cols; i++) {
NSString * columnName = [rscolumnNameForIndex:i];
id columnValue = [rsobjectForColumnIndex:i];
[dict setObject:columnValueforKey:columnName];
}
}
result = dict;
}];
return result;
}
-(NSDictionary *)searchToDB:(NSString *)table dic:(NSDictionary *)where{
__blockNSDictionary *result = nil;
[selfsearchToDB:table dic:where callback:^(NSDictionary *dic) {
if (dic.allKeys.count !=0) {
result = dic;
}
}];
return result;
}
-(void)searchToDB:(NSString *)table dic:(NSDictionary *)where callback:(void(^)(NSDictionary *dic))result{
[queueinDatabase:^(FMDatabase *db) {
NSMutableString* query = [NSMutableStringstringWithFormat:@"SELECT * from %@ ",table];
NSMutableArray* values = [NSMutableArrayarrayWithCapacity:0];
if(where !=nil&& where.count>0){
NSString* wherekey = [selfdictionaryToSqlWhere:whereandValues:values isWhere:YES];
[query appendFormat:@" where %@",wherekey];
}
[selfsqlString:query AddOder:niloffset:0count:1];
FMResultSet* rs =[dbexecuteQuery:query withArgumentsInArray:values];
int num_cols = [rscolumnCount];
NSMutableDictionary *dict = [NSMutableDictionarydictionaryWithCapacity:num_cols];
while ([rsnext]){
for (int i =0; i < num_cols; i++) {
NSString * columnName = [rscolumnNameForIndex:i];
id columnValue = [rsobjectForColumnIndex:i];
[dict setObject:columnValueforKey:columnName];
}
}
result(dict);
}];
}
-(NSString*)dictionaryToSqlWhere:(NSDictionary*)dic andValues:(NSMutableArray*)values isWhere:(BOOL)b{
NSMutableString* wherekey = [NSMutableStringstringWithCapacity:0];
if(dic !=nil && dic.count >0 ){
NSArray* keys = dic.allKeys;
for (int i=0; i< keys.count;i++) {
NSString* key = [keysobjectAtIndex:i];
id va = [dicobjectForKey:key];
if(wherekey.length >0){
[wherekey appendFormat:b ?@" and %@ = ? " : @" , %@ = ? ",key];
}
else{
[wherekey appendFormat:@" %@ = ? ",key];
}
if (values !=nil) {
[values addObject:va];
}
}
}
return wherekey;
}
-(void)sqlString:(NSMutableString*)sql AddOder:(NSString*)orderby offset:(int)offset count:(int)count{
if(orderby !=nil && ![orderby isEmptyWithTrim]){
[sql appendFormat:@" order by %@ ",orderby];
}
if (count >0) {
[sql appendFormat:@" limit %d offset %d ",count,offset];
}
}
- (BOOL)isEmptyWithTrim{
return [[selfstringWithTrim]isEqualToString:@""];
}
- (NSString *)stringWithTrim{
return [selfstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
}
@end
- FMDB的简单封装
- FMDB的简单封装
- 对fmdb简单的封装
- 简单封装FMDB操作sqlite的模板
- iOS简单runtime封装fmdb的使用
- fmdb的封装
- FMDB的二次封装
- ios 对FMDB 简单封装
- FMDB-一个sqlite的封装
- FMDB-一个sqlite的封装
- 对FMDB的封装JRDB
- FMDB的简单使用方法
- FMDB的简单使用
- FMDB的简单使用
- FMDB的简单使用
- FMDB的简单使用
- fmdb 的简单使用
- FMDB的简单使用
- 操作系统的进程状态及linux实例
- Java之枚举用法以及http,http/2的访问返回常识,google浏览器插件
- MySQL学习笔记8:MySQL存储过程(storage procedure)
- OWASP 测试指南 4.0-测试原则
- python基础教程学习笔记十六
- FMDB的简单封装
- RxJava 和 RxAndroid 四(RxBinding的使用)
- Linux中gdb 查看core堆栈信息
- Makefile和Android.mk的基情
- oracle异地恢复
- QMediaPlayer播放视频
- 函数指针与回调
- RxJava 和 RxAndroid 五(线程调度)
- js指定滚动条位置