閑了好久,不知道寫點什么须肆,把最近使用FMDB的過程寫一下吧贴汪!
1. 建立DBManager
建立單例來管理數(shù)據(jù)庫的創(chuàng)建,開啟休吠,及增刪改查操作扳埂。
2. 初始化DataBase,table
使用類屬性創(chuàng)建DataBase及DbQueue
@property (nonatomic, class, readonly) FMDatabase *sharedDataBase;
@property (nonatomic, class, readonly) FMDatabaseQueue *dbQueue;
實現(xiàn)方法
+ (FMDatabase *)sharedDataBase {
static FMDatabase *accountDB = nil;
@synchronized (self) {
if (accountDB == nil) {
NSString *dbPath = [NSHomeDirectory() stringByAppendingFormat:@"/%@",[AccountModel qw_getLocalModel].USERID];
accountDB = [FMDatabase databaseWithPath:dbPath];
if ([accountDB open]) {
DebugLog(@"Database open succeed");
}else{
DebugLog(@"Database open failed");
}
}
}
return accountDB;
}
// 因為FMDB并不是線程安全的瘤礁,所以建立dbQueue來進行線程操作
static FMDatabaseQueue *_dbQueue = nil;
+ (FMDatabaseQueue *)dbQueue {
if (_dbQueue == nil) {
// dbPath和db的path相同阳懂,需要先建立數(shù)據(jù)庫,才能夠成功創(chuàng)建dbQueue柜思;
NSString *dbPath = [NSHomeDirectory() stringByAppendingFormat:@"/%@",[AccountModel qw_getLocalModel].USERID];
_dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
}
return _dbQueue;
}
創(chuàng)建table岩调,根據(jù)保存model的類名來創(chuàng)建不同的table
+ (BOOL)createTabelOfClass:(Class)tableClass {
NSString *excuteStr = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(id, content)", NSStringFromClass(tableClass)];
return [[QWDBManager sharedDataBase] executeStatements:excuteStr];
}
3. 建立Catch
建立一個保存model id的數(shù)組,用來查詢數(shù)據(jù)庫中是否存在該model赡盘,減少內(nèi)存的消耗
@property (nonatomic, strong) NSMutableArray *catchIds;
4. 數(shù)據(jù)存儲号枕,更新
向table中增加model(非線程安全的)
+ (BOOL)addNewModel:(PostModel *)model {
NSString *addStr = [NSString stringWithFormat:@"INSERT INTO %@ VALUES(?,?)",NSStringFromClass(model.class)];
BOOL ret = [[QWDBManager sharedDataBase] executeUpdate:addStr, [NSString stringWithFormat:@"%ld", (long)model.PostID], [model qw_dataOfModel]];
if(ret){
DebugLog(@"Add model succeed");
}else{
DebugLog(@"Add model failed");
}
return ret;
}
查詢table中是否存在model(非線程安全)第三部分catch的存在就是為了替換掉這個操作,如果有刪除model的需要陨享,可以在查詢完成后進行刪除操作葱淳。
+ (BOOL)checkContainModel:(PostModel *)model {
NSString *checkStr = [NSString stringWithFormat:@"SELECT id FROM %@ WHERE id = ?",NSStringFromClass(model.class)];
FMResultSet *resultSet = [[QWDBManager sharedDataBase] executeQuery:checkStr, [NSString stringWithFormat:@"%ld",(long)model.PostID]];
BOOL ret = [resultSet next];
[resultSet close];
return ret;
}
更新model內(nèi)容(非線程安全)
+ (BOOL)updateModel:(PostModel *)model {
BOOL check = [QWDBManager checkContainModel:model];
if (check) {
NSString *updateStr = [NSString stringWithFormat:@"UPDATE %@ SET content = ? WHERE id = %ld",NSStringFromClass(model.class), (long)model.PostID];
BOOL ret = [[QWDBManager sharedDataBase] executeUpdate:updateStr, [model qw_dataOfModel]];
ret ? DebugLog(@"Update succeed") : DebugLog(@"Update failed");
return ret;
}
return [QWDBManager addNewModel:model];
}
** 實際項目中model一般都是網(wǎng)絡(luò)請求到的數(shù)據(jù)轉(zhuǎn)換而來的,所以涉及到線程操作抛姑,而上面這部分操作都不是線程安全的赞厕,所以推薦使用下面的這些方法。**
// 更新model的數(shù)組
+ (void)async_updateModels:(NSArray *)models {
[QWDBManager.dbQueue inDatabase:^(FMDatabase *db) {
[models enumerateObjectsUsingBlock:^(PostModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 檢查catch數(shù)組中是否存在model id
if ([[QWDBManager shareInstance].catchIds containsObject:@(obj.PostID)]) {
[QWDBManager updateModel:obj inDb:db];
}else{
if ([QWDBManager addModels:obj toBb:db]) [[QWDBManager shareInstance].catchIds addObject:@(obj.PostID)]; // 不存在時定硝,將model id 添加到緩存中
}
if (stop) {
// 更新緩存數(shù)據(jù)
[[NSUserDefaults standardUserDefaults] setObject:[QWDBManager shareInstance].catchIds forKey:@"CatchModelIdsKey"];
}
}];
}];
}
+ (BOOL)updateModel:(PostModel *)model inDb:(FMDatabase *)db {
NSString *updateStr = [NSString stringWithFormat:@"UPDATE %@ SET content = ? WHERE id = %ld",NSStringFromClass(model.class), (long)model.PostID];
BOOL ret = [db executeUpdate:updateStr, [model qw_dataOfModel]];
if (ret) {
DebugLog(@"Update model:%ld succeed", (long)model.PostID);
}else{
DebugLog(@"Update model:%ld failed", (long)model.PostID);
}
return ret;
}
+ (BOOL)addModels:(PostModel *)model toBb:(FMDatabase *)db {
NSString *addStr = [NSString stringWithFormat:@"INSERT INTO %@ VALUES(?,?)",NSStringFromClass(model.class)];
BOOL ret = [db executeUpdate:addStr, [NSString stringWithFormat:@"%ld", (long)model.PostID], [model qw_dataOfModel]];
if (ret) {
DebugLog(@"Add model:%ld succeed", (long)model.PostID);
}else{
DebugLog(@"Add model:%ld failed", (long)model.PostID);
}
return ret;
}
恩皿桑!大致就這樣,最后貼個demo地址:QWPapapa , ** 有問題留言**蔬啡。