前言
家里養的是哈士奇,它小的時候,我總喜歡抱著它睡.后來長大了,發現床越來越擠了,就不讓他上床了,這貨叫的比殺豬還慘,沒辦法我只好陪它打地鋪抱著它睡,讓它習慣.可是當我醒來的時候,次噢!這貨為嘛在我床上.
不開心的時候就看的笑話,還記得小時候家里的德國黑貝叫貝貝,長得特別大,平時把它拴上怕它咬人,有時把它放開,它沒有立即跑開去玩,而是拼命的去撕咬栓它的鏈子,當時看了覺得很好笑.它已經離開我們好久了,知道自己得了病,自己找個沒人的地方安靜的死去.
好想養一條呀,應該快了
1.前提工作
安裝cocoapods,并且導入
pod 'FMDB/FTS', '2.5' # sqlite操作
pod 'FCFileManager', '1.0.17' # sqlite操作
pod 'GCJSONKit', '1.5.0' # JSON和對象互轉
2.創建可視化表
首先創建.bundle文件用來裝所有表(便于管理)
然后在.string里面規定自己表定義格式如下圖:
表名:user_friends
主鍵:USER_CODE
字段:數組
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.創建表
/**
* 根據定義創建數據庫表
*
*/
- (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自己研究這里不多說
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.lxweimin.com/p/5dbb07c8d5d5
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.lxweimin.com/p/5dbb07c8d5d5
Demo下載地址:https://github.com/yuhechuan/SqliteTools