转自:csdn博客链接
再封装目的狠明确,就是用的时候更好用。 其实说白了,数据库从大了来说就是增删改查,而我们在项目中用到的基本也就是增删改查的逻辑,简单的封装了下:我的思路就是创建一个单例类,专门用来管理这些接口,具体代码看下边:
YaoFMDBManager.h
#import
typedef void(^getDataByPrimaryKey)(NSData *data);
typedef void(^getAllData)(NSArray *dataArr);
@interface YaoFMDBManager : NSObject
+ (YaoFMDBManager *)sharedInstance;
//add the data by primaryKey in table
- (BOOL)addDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(NSData *)data;
//delete the data by primaryKey in table
- (BOOL)deleteDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey;
//update data by primaryKey in table
- (BOOL)updateDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(NSData *)data;
//read the data by primaryKey in table
- (BOOL)readDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(getDataByPrimaryKey)dataBlock;
//read all the data in table
- (BOOL)readAllDataWithTableName:(NSString *)tableName dataArr:(getAllData)allDataBlock;
//clear the dataBase
- (BOOL)clearDataBaseWithTableName:(NSString *)tableName;
@end
YaoFMDBManager.m
#import "YaoFMDBManager.h" #import "FMDatabase.h" #define CREAT_TABLE_IFNOT_EXISTS @"create table if not exists %@ (key text primary key, data blob)" #define DELETE_DATA_WITH_PRIMARYKEY @"delete from %@ where key = ?" #define INSERT_TO_TABLE @"insert into %@ (key, data) values (?, ?)" #define READ_DATA_TABLE_WITH_PRIMARYKEY @"select data from %@ where key = ?" #define READ_ALL_DATA @"select data from %@" #define UPDATE_DATA_WHTH_PRIMARYKEY @"update %@ set data = ? where key = ?" #define CLEAR_ALL_DATA @"DELETE FROM %@" @implementation YaoFMDBManager + (YaoFMDBManager *)sharedInstance { static YaoFMDBManager *YaoManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ YaoManager = [[YaoFMDBManager alloc] init]; }); return YaoManager; } - (BOOL)addDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(NSData *)data { BOOL ret = false; //get the dB path NSString *dbPath = [self getDbPath]; //init a dataBase FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; if ([dataBase open]) { NSString *sql = [NSString stringWithFormat:CREAT_TABLE_IFNOT_EXISTS,tableName]; ret = [dataBase executeUpdate:sql]; if (ret) { //make sure the primaryKey is uinque. NSString *deleteSql = [NSString stringWithFormat:DELETE_DATA_WITH_PRIMARYKEY,tableName]; ret = [dataBase executeUpdate:deleteSql,primaryKey]; if (ret) { NSString *storeURL = [NSString stringWithFormat:INSERT_TO_TABLE,tableName]; ret = [dataBase executeUpdate:storeURL,primaryKey,data]; } } } [dataBase close]; return ret; } - (BOOL)deleteDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey { BOOL ret = false; NSString *dbPath = [self getDbPath]; FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; if ([dataBase open]) { NSString *sql = [NSString stringWithFormat:CREAT_TABLE_IFNOT_EXISTS,tableName]; ret = [dataBase executeUpdate:sql,primaryKey]; if (ret) { NSString *deleteSql = [NSString stringWithFormat:DELETE_DATA_WITH_PRIMARYKEY,tableName]; ret = [dataBase executeUpdate:deleteSql,primaryKey]; } } [dataBase close]; return ret; } - (BOOL)updateDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(NSData *)data { BOOL ret = false; NSString *dbPath = [self getDbPath]; FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; if ([dataBase open]) { NSString *sql = [NSString stringWithFormat:CREAT_TABLE_IFNOT_EXISTS,tableName]; ret = [dataBase executeUpdate:sql,primaryKey]; if (ret) { NSString *updateSql = [NSString stringWithFormat:UPDATE_DATA_WHTH_PRIMARYKEY,tableName]; ret = [dataBase executeUpdate:updateSql,data,primaryKey]; } } [dataBase close]; return ret; } - (BOOL)readDataWithTableName:(NSString *)tableName primaryKey:(NSString *)primaryKey data:(getDataByPrimaryKey)dataBlock { BOOL ret = false; NSString *dataPath = [self getDbPath]; FMDatabase *dB = [FMDatabase databaseWithPath:dataPath]; if ([dB open]) { NSString *creatSql = [NSString stringWithFormat:CREAT_TABLE_IFNOT_EXISTS,tableName]; ret = [dB executeUpdate:creatSql]; if (ret) { NSString *readSql = [NSString stringWithFormat:READ_DATA_TABLE_WITH_PRIMARYKEY,tableName]; FMResultSet *resultSet = [dB executeQuery:readSql,primaryKey]; while ([resultSet next]) { NSData *data = [resultSet dataForColumn:@"data"]; dataBlock(data); } } } [dB close]; return ret; } - (BOOL)readAllDataWithTableName:(NSString *)tableName dataArr:(getAllData)allDataBlock { BOOL ret = false; NSString *dbPath = [self getDbPath]; NSMutableArray *dataArr = [NSMutableArray array]; FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; if ([dataBase open]) { NSString *sql = [NSString stringWithFormat:CREAT_TABLE_IFNOT_EXISTS,tableName]; ret = [dataBase executeUpdate:sql]; if (ret) { NSString *readSql = [NSString stringWithFormat:READ_ALL_DATA,tableName]; FMResultSet *result = [dataBase executeQuery:readSql]; while ([result next]) { NSData *data = [result dataForColumn:@"data"]; [dataArr addObject:data]; } allDataBlock(dataArr); } } [db close]; return ret; } - (BOOL)clearDataBaseWithTableName:(NSString *)tableName{ BOOL ret = false; NSString *dbPath = [self getDbPath]; FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath]; if ([dataBase open]) { NSString *clearSql = [NSString stringWithFormat:CLEAR_ALL_DATA,tableName]; ret = [dataBase executeUpdate:clearSql]; } [dataBase close]; return ret; } #pragma mark - private function - (NSString *)getDbPath { NSString *doc= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; return [doc stringByAppendingString:@"data.sqlite"]; } @end
FMDBManager的使用:
NSDictionary *map = [NSDictionary dictionaryWithObjectsAndKeys:@[@111.222,@333.444],@"上海", @[@112.111,@222.222],@"重庆",@[@333.444,@555.666],@"北京",nil]; NSArray *arr = @[@"123",@"234"]; NSArray *updateArr = @[@"888888",@"9999999"]; NSData *dicData = [NSJSONSerialization dataWithJSONObject:map options:NSJSONWritingPrettyPrinted error:nil]; NSData *arrData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil]; NSData *updateArrData = [NSJSONSerialization dataWithJSONObject:updateArr options:NSJSONWritingPrettyPrinted error:nil]; //增加一条 [[YaoFMDBManager sharedInstance] addDataWithTableName:@"student" primaryKey:@"yao" data:arrData]; // 再增加一条 [[YaoFMDBManager sharedInstance] addDataWithTableName:@"student" primaryKey:@"tian" data:dicData]; //删除primary key为‘yao’的数据 [[YaoFMDBManager sharedInstance] deleteDataWithTableName:@"student" primaryKey:@"yao"]; //更新primary key为‘yao’的数据 [[YaoFMDBManager sharedInstance] updateDataWithTableName:@"student" primaryKey:@"yao" data:updateArrData]; //读取primary key为'yao'的数据 __block NSArray *dataArr = [NSArray array]; [[YaoFMDBManager sharedInstance] readDataWithTableName:@"student" primaryKey:@"yao" data:^(NSData *data) { dataArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; }]; //读取primary key为'tian'的数据 __block NSDictionary *dict = [NSDictionary dictionary]; [[YaoFMDBManager sharedInstance] readDataWithTableName:@"student" primaryKey:@"tian" data:^(NSData *data) { dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; }]; //读取整个表的数据 __block NSMutableArray *allArr = [NSMutableArray array]; [[YaoFMDBManager sharedInstance] readAllDataWithTableName:@"student" dataArr:^(NSArray *dataArr) { for (int i = 0; i