码迷,mamicode.com
首页 > 数据库 > 详细

数据存储之第三方FMDB优化

时间:2016-01-15 09:49:02      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

    最近项目要用到数据库,采用的是第三方FMDB, 之前做C#时用过sqlHelper,自己就按着sqlHelper的思路封装了一下,封装的也比较简单,看到网上有一些根据FMDB封装的ORM框架,但基本都是单表的.

按着ADO.Net的思路

1.连接数据库

2.打开数据库

3.操作数据库

4.关闭数据库

//
//  FMDBManager.h
//  XQBCommunityApp
//
//  Created by City--Online on 16/1/14.
//  Copyright © 2016年 CityOnline_1. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "FMDB.h"

typedef  void(^TransactionBlock)(BOOL success);

@interface FMDBManager : NSObject

-(BOOL)updateDataBase:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;

-(void)openDatabase;

-(void)closeDatabase;

-(FMResultSet *)QueryDataBase:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;

//事务成功与否block
@property (nonatomic,copy) TransactionBlock  transactionBlock;

-(void)updateDataBaseInTransaction:(NSArray *)sqlArray;

@end
//
//  FMDBManager.m
//  XQBCommunityApp
//
//  Created by City--Online on 16/1/14.
//  Copyright © 2016年 CityOnline_1. All rights reserved.
//

#import "FMDBManager.h"
#import "FMDB.h"
/**
 *  数据库名字
 */
#define XQB_DB_NAME       @"XqbDB.sqlite"
#define XQB_DB_PATH       @"XqbDbDir"


@interface FMDBManager ()
@property (nonatomic,strong) NSString *tablePath;
@property (nonatomic,strong) FMDatabase *database;
@end
@implementation FMDBManager

- (void)connectDataBase
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *databasePath = [NSString stringWithFormat:@"%@/%@", pathDocuments,XQB_DB_PATH];
    
    // 判断文件夹是否存在,如果不存在,则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:databasePath]) {
        [fileManager createDirectoryAtPath:databasePath withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"FileDir is exists.");
    }
    //获取数据库 的路径
    _tablePath = [NSString stringWithFormat:@"%@/%@",databasePath,XQB_DB_NAME];
    _database = [FMDatabase databaseWithPath:_tablePath];
    NSLog(@"%@",_tablePath);
}

-(BOOL)updateDataBase:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
{
    return [_database executeUpdate:sql withArgumentsInArray:arguments];
}

-(FMResultSet *)QueryDataBase:(NSString *)sql withArgumentsInArray:(NSArray *)arguments
{
    FMResultSet *set=nil;
    set=[_database executeQuery:sql withArgumentsInArray:arguments];
    return set;
}

-(void)openDatabase
{
    //创建数据库
    [self connectDataBase];
    [_database open];
}
-(void)closeDatabase
{
    [_database close];
}

-(void)updateDataBaseInTransaction:(NSArray *)sqlArray
{
    FMDatabaseQueue *queue=[FMDatabaseQueue databaseQueueWithPath:_tablePath];
    [queue inDatabase:^(FMDatabase *db) {
        @try {
            for (NSString *sql in sqlArray) {
                [db executeUpdate:sql];
            }
        }
        @catch (NSException *exception) {
            [db rollback];
            if (self.transactionBlock) {
                _transactionBlock(NO);
            }
        }
        @finally {
            [db commit];
            if (self.transactionBlock) {
                _transactionBlock(YES);
            }
        }
    }];
}

@end

 

数据存储之第三方FMDB优化

标签:

原文地址:http://www.cnblogs.com/5ishare/p/5132365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!