iOS-FMDB本地存儲之一種封裝思想

1.前提工做git

安裝cocoapods,而且導入github

pod 'FMDB/FTS', '2.5'               # sqlite操做
pod 'FCFileManager', '1.0.17'       # sqlite操做
pod 'GCJSONKit', '1.5.0'            # JSON和對象互轉

2.建立可視化表sql

首先建立.bundle文件用來裝全部表(便於管理)數據庫

clipboard.png

而後在.string裏面規定本身表定義格式以下圖:
表名:user_friends
主鍵:USER_CODE
字段:數組數組

clipboard.png

3.加載表定義安全

建立一個manager 來管理表,加載表定義,須要傳入你.bundle文件的名字和數據庫存儲路徑.多線程

/**
 * 初始化數據對象
 * @param defineFileName :  數據庫定義文件
 * @param  filePath : 數據庫文件路徑
 */
- (instancetype)initWithDBDefine:(NSString * )bundleName filePath:(NSString *)filePath {
    if (self =[super init]) {
        self.dbFile = filePath;
        [self loadTableDefinition:bundleName];
    }
    return self;
}

4.建立表app

/**
 * 根據定義建立數據庫表
 *
 */
- (BOOL)createTableWithConfig:(NSDictionary *)tableDefine withDb:(FMDatabase *)db

數據庫建立表具體sql語句學習

CREATE TABLE IF NOT EXISTS user_friends(USER_CODE TEXT primary key not null,USER_NAME TEXT,FRIEND_CODE TEXT,USER_IMG_SRC TEXT,USER_SHORT_NAME TEXT,USER_EN_NAME TEXT,USER_SEX TEXT,DEPT_NAME TEXT,USER_POST TEXT)

你所須要的是表名,主鍵,及各類字段名字拼成以上格式,這裏很少說能夠文章底部github下載demo本身研究這裏很少說ui

5.打開數據庫的管理類

在這建立manger,並傳入存儲路徑
保證數據庫,操做在同一線程操做,當咱們在程序中運用到多線程的時候,那麼你必需要考慮的就是各線程搶佔資源的問題,不能讓同一時間多個線程去搶一個資源,好比你兩個線程同時去操做sql,就會形成有髒讀數據或者查不到數據,或者查的是髒數據.

#import "SQliteUser.h"

@interface SQliteUser ()

@property (nonatomic, strong) SqliteManager *manager;

@end

@implementation SQliteUser

SINGLETON_FOR_CLASS(SQliteUser);//宏單例

- (SqliteManager *)manager {
    if (_manager) {
        return _manager;
    }
    @synchronized(self) {
        if (!_manager) {
             NSString *dbPath = [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/Documents/yuhechuan.db"];
            _manager = [[SqliteManager alloc] initWithDBDefine:@"user" filePath:dbPath];
        }
    }
    return _manager;
}

@end

synchronized同步鎖,保證一個線程執行完成,再執行其餘線程任務
通常說synchronized是加鎖,或者說是加對象鎖,其實對象鎖只是synchronized在實現鎖機制中的一種鎖(重量鎖,用這種方式互斥線程開銷大因此叫重量鎖,或者叫對象monitor),而synchronized的鎖機制會根據線程競爭狀況在運行會有偏向鎖、輕量鎖、對象鎖,自旋鎖(或自適應自旋鎖)等
參考連接:http://www.jianshu.com/p/5dbb...

6.建立DAO

@implementation SQBaseDAO

子類繼承baseDAO 實現下面兩個父類的方法,來告訴manger,表名和主鍵

-(NSString *)getPK {
    return @"須要子類實現";
}
- (NSString *)getTable {
    return @"須要子類實現";
}

獲取Sqlite管理類和其中的FMDatabaseQueue

-(SqliteManager *)getDataSource {
    return [[SQliteUser sharedInstance] manager];
}

7.實現增,刪,改,查

首先調用數據庫的方法應在FMDB提供的方法裏面執行

-(void)inDatabase:(void (^)(FMDatabase *db))block

這個方法提供了一個代碼塊。操做數據庫的代碼寫在block裏,如:
FMDatabaseQueue是一個串行隊列,它不支持串行任務嵌套執行
如下代碼比較簡單不予多說本身看

//增
- (BOOL)insert:(NSDictionary *)data {
    //安全線程
    FMDatabaseQueue *dbQueue = [[self getDataSource] dbQueue];
    __block BOOL execute = NO;
    [dbQueue inDatabase:^(FMDatabase *db) {
        NSMutableDictionary *mData = [NSMutableDictionary dictionaryWithDictionary:data];
        NSString *pk = [self getPK];//主鍵
        NSString * table = [self getTable];//表名
        //若是主鍵沒值,主動生成主鍵
        if (![mData objectForKey:pk]) {
            [mData setObject:[[NSUUID UUID] UUIDString] forKey:pk];
        }
        NSMutableString *insertKey = [NSMutableString stringWithCapacity:0];
        NSMutableString *insertValuesString = [[NSMutableString alloc] init];
        NSMutableArray *insertValues = [[NSMutableArray alloc] init];
        NSArray *columnArray = [mData allKeys];
        for (int i = 0; i < columnArray.count; i++) {
            NSString *columnName = columnArray[i];
            id value = [mData objectForKey:columnName];
            if (!value) {
                continue;
            }
            if (insertKey.length > 0) {
                [insertKey appendString:@","];
                [insertValuesString appendString:@","];
            }
            
            [insertKey appendString:columnName];
            [insertValuesString appendString:@"?"];
            
            [insertValues addObject:value];
        }
        // 拼接insertSQL 語句  採用 replace 插入
        NSString *insertSQL = [NSString stringWithFormat:@"replace into %@(%@) values(%@)", table, insertKey, insertValuesString];
        execute = [db executeUpdate:insertSQL withArgumentsInArray:insertValues];
        //打印日誌,若是在主線程執行,方便查找
        [self printSQLLog:insertSQL values:insertValues];
        
    }];
    return execute;
}

//刪
- (BOOL)remove:(SQConditionBean *)condition {
    FMDatabaseQueue *dbQueue = [[self getDataSource] dbQueue];
    __block BOOL execute = NO;
    [dbQueue inDatabase:^(FMDatabase *db) {
        NSString *table = [self getTable];
        NSMutableString *deleteSQL = [NSMutableString stringWithFormat:@"delete from %@  ", table];
        // 添加where 語句
        NSMutableArray *valuearray = [NSMutableArray array];
        //獲取條件字符串
        NSDictionary *dict = [condition conditionDict];
        NSString *sqlwhere = [self dictionaryToSqlWhere:dict andValues:valuearray];
        if (sqlwhere.length > 0) {
            [deleteSQL appendString:@" where "];
            [deleteSQL appendString:sqlwhere];
        }
        execute = [db executeUpdate:deleteSQL withArgumentsInArray:valuearray];
        //打印日誌,若是在主線程執行,方便查找
        [self printSQLLog:deleteSQL values:valuearray];

    }];
    return execute;
}

//改
- (BOOL)modify:(NSDictionary *)data {
    FMDatabaseQueue *dbQueue = [[self getDataSource ] dbQueue];
    __block BOOL execute = NO;
    [dbQueue inDatabase:^(FMDatabase *db) {
        NSString *pk = [self getPK];//主鍵
        NSString * table = [self getTable];//表名
        NSMutableString *updateKey = [NSMutableString string];//更新key集合
        NSMutableArray *updateValues = [[NSMutableArray alloc] init];//更新的value集合
        NSArray *columnArray = data.allKeys;
        for (NSString *key in columnArray) {
            //不是主鍵字段
            if (![key isEqualToString:pk]) {
                if (updateKey.length > 0) {
                    [updateKey appendString:@","];
                }
                [updateKey appendFormat:@"%@=?", key];
            }
            id value = [data objectForKey:key];
            [updateValues addObject:value];
        }
        NSMutableString *updateSQL = [NSMutableString stringWithFormat:@"update %@ set %@ where %@=?", table, updateKey,[updateValues lastObject]];
        execute = [db executeUpdate:updateSQL withArgumentsInArray:updateValues];
        //打印日誌,若是在主線程執行,方便查找
        [self printSQLLog:updateSQL values:updateValues];
    }];
    return execute;
}

//查
    - (NSMutableArray *)query:(SQConditionBean *)condition{
        FMDatabaseQueue *dbQueue = [[self getDataSource ] dbQueue];
        __block NSMutableArray *results = [[NSMutableArray alloc] init];
        [dbQueue inDatabase:^(FMDatabase *db) {
            NSString * table = [self getTable];
            NSString *columnsString = @"*";
            
            //build query
            NSMutableString *query = [NSMutableString stringWithFormat:@"select %@ from %@", columnsString, table];
            NSMutableArray *whereValues = [NSMutableArray array];
            
            //build where
            NSString *wherekey = [self dictionaryToSqlWhere:[condition conditionDict] andValues:whereValues];
            if ( wherekey.length > 0) {
                [query appendFormat:@" where %@", wherekey];
            }
            
            //execute
            FMResultSet *set = nil;
            if (whereValues.count == 0) {
                set = [db executeQuery:query];
            } else {
                set = [db executeQuery:query withArgumentsInArray:whereValues];
            }
            //打印日誌,若是在主線程執行,方便查找
            [self printSQLLog:query values:whereValues];
            
            
            while ([set next]) {
                NSDictionary *rowData = [self resultToDic:set];
                [results addObject:rowData];
            }
            [set close];
    
        }];
        return results;
    }

8.建立條件SQConditionBean 拼接where語句

攜帶一個字典

@property (nonatomic, strong) NSMutableDictionary *conditionDict;
/**
 *  添加條件(操做符是AND)
 *
 *  @param value 值
 *  @param key   字段
 */
-(void) set:(id)value forKey:(NSString *)key {
    if (value) {
        [conditionDict setValue:value forKey:key];
    }
}
/**
 *  添加  != 條件 (操做符是AND)
 *
 *  @param value 值
 *  @param key   字段
 */
-(void) andNE:(id)value forKey:(NSString *)key {
    NSString * query =  [NSString stringWithFormat:@"%@ != ", key];
    [conditionDict setValue:value forKey:query];
}

參考資料:http://www.jianshu.com/p/5dbb...
Demo下載地址:https://github.com/yuhechuan/...


版權地址:https://www.jianshu.com/p/9ff...

做者:有毒的程序猿

說明:八點鐘學院二期學員

學習連接:https://ke.qq.com/course/171725

交流羣:272306631

相關文章
相關標籤/搜索