一、FMDB簡介
- 什么是FMDB
- FMDB是
iOS平臺(tái)
的SQLite數(shù)據(jù)庫
框架
- FMDB以
面向OC
的方式封裝了SQLite的C語言API
- FMDB是
-
FMDB優(yōu)點(diǎn)
- 使用起來更加面向?qū)ο螅∪チ撕芏嗦闊┣堆浴⑷哂嗟腃語言代碼
- 對(duì)比蘋果自帶的Core Data框架蝠检,更加輕量級(jí)和靈活
- 提供了多線程安全的數(shù)據(jù)庫操作方法怖亭,有效地防止數(shù)據(jù)混亂
-
FMDB的github地址
-
核心類
- FMDatabase
- 一個(gè)FMDatabase對(duì)象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫
- 用來執(zhí)行SQL語句
- FMDatabase這個(gè)類是線程不安全的涎显,如果在多個(gè)線程中同時(shí)使用一個(gè)FMDatabase實(shí)例,會(huì)造成數(shù)據(jù)混亂等問題
- 為了保證線程安全兴猩,F(xiàn)MDB提供方便快捷的FMDatabaseQueue類
- FMResultSet
- 使用FMDatabase執(zhí)行查詢后的結(jié)果集
- FMDatabaseQueue
- 用于在多線程中執(zhí)行多個(gè)查詢或更新期吓,它是線程安全的
// FMDatabaseQueue的創(chuàng)建
FMDatabaseQueue *queue =
[FMDatabaseQueue databaseQueueWithPath:path];
[queue inDatabase:^(FMDatabase*db) {
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jack"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Rose"];
[db executeUpdate:@"INSERTINTO t_student(name)VALUES (?)",@"Jim"];
FMResultSet*rs= [dbexecuteQuery:@"select* from t_student"];
while([rsnext]) {
// …
}
}];
```
+ 注意:打開數(shù)據(jù)庫,文件路徑有三種情況
- 具體文件路徑:如果不存在會(huì)自動(dòng)創(chuàng)建
- 空字符串@"":會(huì)在臨時(shí)目錄創(chuàng)建一個(gè)空的數(shù)據(jù)庫倾芝,當(dāng)FMDatabase連接關(guān)閉時(shí)讨勤,數(shù)據(jù)庫文件也被刪除‘
- nil:會(huì)創(chuàng)建一個(gè)內(nèi)存中臨時(shí)數(shù)據(jù)庫,當(dāng)FMDatabase連接關(guān)閉時(shí)晨另,數(shù)據(jù)庫會(huì)被銷毀
```objc
// 通過指定SQLite數(shù)據(jù)庫文件路徑來創(chuàng)建FMDatabase對(duì)象
FMDatabase*db= [FMDatabase databaseWithPath:path];
if (![dbopen]) {
NSLog(@"數(shù)據(jù)庫打開失斕肚А!");
}
- 在FMDB中借尿,除查詢以外的所有操作刨晴,都稱為“更新”:create、drop路翻、insert狈癞、update、delete等
// 使用executeUpdate:方法執(zhí)行更新
- (BOOL)executeUpdate:(NSString*)sql, ...
- (BOOL)executeUpdateWithFormat:(NSString*)format,...
- (BOOL)executeUpdate:(NSString*)sqlwithArgumentsInArray:(NSArray*)arguments
- 執(zhí)行查詢
- (FMResultSet*)executeQuery:(NSString*)sql, ...
- (FMResultSet*)executeQueryWithFormat:(NSString*)format,...
- (FMResultSet*)executeQuery:(NSString*)sqlwithArgumentsInArray:(NSArray*)arguments
二茂契、CoreData 與 FMDB
- coreData:自動(dòng)生成SQL語句蝶桶,不能自己設(shè)置,個(gè)性化的掉冶、優(yōu)化的等SQL語句不能實(shí)現(xiàn)
- FMDB:可以自己定制不同的SQL語句真竖,擴(kuò)展性好
三、FMDB實(shí)例 基本使用
- 1.導(dǎo)入FMDB框架到項(xiàng)目中
- 2.使用FMDB
# 二厌小、增刪改查
+ 數(shù)據(jù)庫操作都放在queue中的疼邀,queue中操作database,線程是安全的
```objc
#import "JPViewController.h"
#import "FMDB.h"
@interface JPViewController ()
@property (nonatomic, strong) FMDatabase *db;
@end
@implementation JPViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.獲得數(shù)據(jù)庫文件的路徑
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename = [doc stringByAppendingPathComponent:@"students.sqlite"];
// 2.得到數(shù)據(jù)庫
FMDatabase *db = [FMDatabase databaseWithPath:filename];
// 3.打開數(shù)據(jù)庫
if ([db open]) {
// 4.創(chuàng)表
BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
if (result) {
NSLog(@"成功創(chuàng)表");
} else {
NSLog(@"創(chuàng)表失敗");
}
}
self.db = db;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self delete];
[self insert];
[self query];
}
- (void)insert
{
for (int i = 0; i<10; i++) {
NSString *name = [NSString stringWithFormat:@"jack-%d", arc4random_uniform(100)];
// executeUpdate : 不確定的參數(shù)用?來占位召锈,參數(shù)都必須是對(duì)象
[self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", name, @(arc4random_uniform(40))];
// [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);" withArgumentsInArray:@[name, @(arc4random_uniform(40))]]; // ?方式旁振,參數(shù)要是對(duì)象,不是對(duì)象要包裝為對(duì)象
// executeUpdateWithFormat : format方式拼接涨岁,不確定的參數(shù)用%@拐袜、%d等來占位
// [self.db executeUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];
// 注意:如果直接寫%@不用加單引號(hào)兩邊,會(huì)自動(dòng)加單引號(hào)兩邊梢薪,如果'jack_%d'的兩邊不加單引號(hào)就會(huì)報(bào)錯(cuò)蹬铺,所以要注意
[self.db executeUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES ('jack_%d', %d);", name, arc4random_uniform(40)];
}
}
- (void)delete
{
// [self.db executeUpdate:@"DELETE FROM t_student;"];
[self.db executeUpdate:@"DROP TABLE IF EXISTS t_student;"];
[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
}
- (void)query
{
// 1.執(zhí)行查詢語句
FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_student"];
// 2.遍歷結(jié)果
while ([resultSet next]) {
int ID = [resultSet intForColumn:@"id"];
NSString *name = [resultSet stringForColumn:@"name"];
int age = [resultSet intForColumn:@"age"];
NSLog(@"%d %@ %d", ID, name, age);
}
}
@end
+ 事務(wù)操作
```objc
#import "JPViewController.h"
#import "FMDB.h" // 導(dǎo)入頭文件
@interface JPViewController ()
@property (nonatomic, strong) FMDatabaseQueue *queue;
@end
@implementation JPViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.獲得數(shù)據(jù)庫文件的路徑
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename = [doc stringByAppendingPathComponent:@"persons.sqlite"];
// 2.得到數(shù)據(jù)庫
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filename];
// 3.打開數(shù)據(jù)庫
[queue inDatabase:^(FMDatabase *db) {
// 4.創(chuàng)表
BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_person (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
if (result) {
NSLog(@"成功創(chuàng)表");
} else {
NSLog(@"創(chuàng)表失敗");
}
}];
self.queue = queue;
}
// 事務(wù)操作:錯(cuò)誤會(huì)回滾
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self.queue inDatabase:^(FMDatabase *db) {
// [db beginTransaction];
//
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
//
// [db commit];
// }];
// [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);", @"jake", @30];
// }];
}
@end
四、FMDB緩存數(shù)據(jù)
#import "JPStatusTool.h"
#import "JPHttpTool.h"
#import "MJExtension.h"
#import "FMDB.h"
#import "JPStatus.h"
@implementation JPStatusTool
/** 數(shù)據(jù)庫實(shí)例 */
static FMDatabase *_db;
+ (void)initialize
{
// 1.獲得數(shù)據(jù)庫文件的路徑
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename = [doc stringByAppendingPathComponent:@"status.sqlite"];
// 2.得到數(shù)據(jù)庫
_db = [FMDatabase databaseWithPath:filename];
// 3.打開數(shù)據(jù)庫
if ([_db open]) {
// 4.創(chuàng)表
BOOL result = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_home_status (id integer PRIMARY KEY AUTOINCREMENT, access_token text NOT NULL, status_idstr text NOT NULL, status_dict blob NOT NULL);"];
if (result) {
NSLog(@"成功創(chuàng)表");
} else {
NSLog(@"創(chuàng)表失敗");
}
}
}
+ (void)homeStatusesWithParam:(JPHomeStatusesParam *)param success:(void (^)(JPHomeStatusesResult *))success failure:(void (^)(NSError *))failure
{
// param.count = @5;
// 從數(shù)據(jù)庫中讀缺病(加載)緩存數(shù)據(jù)(微博模型數(shù)組)
NSArray *cachedHomeStatuses = [self cachedHomeStatusesWithParam:param];
if (cachedHomeStatuses.count != 0) { // 有緩存數(shù)據(jù)
if (success) {
JPHomeStatusesResult *result = [[JPHomeStatusesResult alloc] init];
result.statuses = cachedHomeStatuses;
success(result);
}
} else { // 沒有緩存數(shù)據(jù)
NSDictionary *params = [param keyValues];
[JPHttpTool get:@"https://api.weibo.com/2/statuses/home_timeline.json" params:params success:^(id responseObj) {
// 新浪返回的字典數(shù)組
NSArray *statusDictArray = responseObj[@"statuses"];
// 緩存微博字典數(shù)組
[self saveHomeStatusDictArray:statusDictArray accessToken:param.access_token];
if (success) {
JPHomeStatusesResult *result = [JPHomeStatusesResult objectWithKeyValues:responseObj];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
}
/**
* 通過請(qǐng)求參數(shù)去數(shù)據(jù)庫中加載對(duì)應(yīng)的微博數(shù)據(jù)
*
* @param param 請(qǐng)求參數(shù)
*/
+ (NSArray *)cachedHomeStatusesWithParam:(JPHomeStatusesParam *)param
{
// 創(chuàng)建數(shù)組緩存微博數(shù)據(jù)
NSMutableArray *statuses = [NSMutableArray array];
// 根據(jù)請(qǐng)求參數(shù)查詢數(shù)據(jù)
FMResultSet *resultSet = nil;
if (param.since_id) {
resultSet = [_db executeQuery:@"SELECT * FROM t_home_status WHERE access_token = ? AND status_idstr > ? ORDER BY status_idstr DESC limit ?;", param.access_token, param.since_id, param.count];
} else if (param.max_id) {
resultSet = [_db executeQuery:@"SELECT * FROM t_home_status WHERE access_token = ? AND status_idstr <= ? ORDER BY status_idstr DESC limit ?;", param.access_token, param.max_id, param.count];
} else {
resultSet = [_db executeQuery:@"SELECT * FROM t_home_status WHERE access_token = ? ORDER BY status_idstr DESC limit ?;", param.access_token, param.count];
}
// 遍歷查詢結(jié)果
while (resultSet.next) {
NSData *statusDictData = [resultSet objectForColumnName:@"status_dict"];
NSDictionary *statusDict = [NSKeyedUnarchiver unarchiveObjectWithData:statusDictData];
// 字典轉(zhuǎn)模型
JPStatus *status = [JPStatus objectWithKeyValues:statusDict];
// 添加模型到數(shù)組中
[statuses addObject:status];
}
return statuses;
}
/**
* 緩存微博字典數(shù)組到數(shù)據(jù)庫中
*/
+ (void)saveHomeStatusDictArray:(NSArray *)statusDictArray accessToken:(NSString *)accessToken
{
for (NSDictionary *statusDict in statusDictArray) {
// 把statusDict字典對(duì)象序列化成NSData二進(jìn)制數(shù)據(jù)
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:statusDict];
[_db executeUpdate:@"INSERT INTO t_home_status (access_token, status_idstr, status_dict) VALUES (?, ?, ?);",
accessToken, statusDict[@"idstr"], data];
}
}
@end