时间:2021-07-01 10:21:17 帮助过:4人阅读
// // 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优化
标签: