任何的開發(fā)都或多或少的接觸到數(shù)據(jù)庫,而在IOS中一般使用的是SQLite數(shù)據(jù)庫,這是一個輕量功能較為不錯的數(shù)據(jù)庫.而現(xiàn)在用到比較多的第三方數(shù)據(jù)庫操作框架就是FMDB.廢話不多說,相信查找到這篇文章的都是已經(jīng)上手的IOS開發(fā)者,直接上一些相關(guān)使用.
首先簡單介紹下
實現(xiàn)客戶端數(shù)據(jù)庫操作的第三方框架.
操作數(shù)據(jù)庫的類 : FMDatabase.h
隊列調(diào)度數(shù)據(jù)庫執(zhí)行的類 : FMDatabaseQueue.h
查詢數(shù)據(jù)的類 : FMResultSet.h
注意
第三方的官方文檔是這么說的:
FMDatabaseQueue - If you're wanting to perform queries and updates on multiple threads, you'll want to use this class. It's described in the "Thread Safety" section below.
翻譯:如果你像去實現(xiàn)查找和更新在多線程上,你就是用這個類,這個是建立在線程安全之上的.
做法:一般自行封裝成一個工具類,設(shè)計成單例.
目的:全局只有一個串行隊列,這樣操作數(shù)據(jù)庫更加安全.
先創(chuàng)建.h文件
@interface FMDatabaseQueueManager : FMDatabaseQueue
//提供單例入口
+ (instancetype)sharedManager;
@end
在.m文件中實現(xiàn)方法,創(chuàng)建一個數(shù)據(jù)庫
+(instancetype)sharedManager{
static FMDatabaseQueueManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 1.獲取數(shù)據(jù)庫路徑,將要把數(shù)據(jù)庫存到這個路徑中.
NSString *SQLPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"my.db"];
// 2. 創(chuàng)建管理數(shù)據(jù)庫對象
instance = [FMDatabaseQueueManager databaseQueueWithPath:SQLPath];
});
return instance;
}
新建表
[[FMDatabaseQueueManager sharedManager] inDatabase:^(FMDatabase *db) {
BOOL isCreate = [db executeUpdate:@"create table if not exists t_person(id integer primary key,name text not null,age integer);"];
使用事務(wù)插入數(shù)據(jù)
// 1.定義SQL語句
NSString *insertSQL = @"insert into t_person(name,age) values(?,?);";
// 2.隊列調(diào)度數(shù)據(jù)庫,使用事務(wù)插入數(shù)據(jù),性能比原生SQLite3要好
[[FMDatabaseQueueManager sharedManager] inTransaction:^(FMDatabase *db, BOOL *rollback) {
// 3.循環(huán)新增大批量的數(shù)據(jù)
for (NSInteger i = 0; i < 1000; i++) {
// 4.執(zhí)行SQL
BOOL isOK = [db executeUpdate:insertSQL,@"張三",@(100)];
// 出錯就回滾
if (!isOK) {
// 回滾快照
*rollback = YES;
// 非常重要
break;
}
}
}];
查詢數(shù)據(jù)
// 1.定義SQL語句
NSString *updateSQL = @"select name,age from t_person;";
// 2.隊列調(diào)度數(shù)據(jù)庫
[[FMDatabaseQueueManager sharedManager] inDatabase:^(FMDatabase *db) {
// 3.執(zhí)行SQL
FMResultSet *resultSet = [db executeQuery:updateSQL];
// 4.逐條取記錄
while ([resultSet next]) {
NSString *name = [resultSet stringForColumn:@"name"];
int age = [resultSet intForColumn:@"age"];
NSLog(@"%@ -- %d",name,age);
}
}];
刪除數(shù)據(jù)
// 1.定義SQL語句
NSString *updateSQL = @"delete from t_person where id = ?;";
// 2.隊列調(diào)度數(shù)據(jù)庫
[[FMDatabaseQueueManager sharedManager] inDatabase:^(FMDatabase *db) {
// 3.執(zhí)行SQL
BOOL isOK = [db executeUpdate:updateSQL,@(6)];
if (isOK) {
NSLog(@"刪除數(shù)據(jù)成功");
} else {
NSLog(@"刪除數(shù)據(jù)失敗");
}
}];
修改數(shù)據(jù)
// 1.定義SQL語句
NSString *updateSQL = @"update t_person set name = ?,age = ? where id = ?;";
// 2.隊列調(diào)度數(shù)據(jù)庫
[[FMDatabaseQueueManager sharedManager] inDatabase:^(FMDatabase *db) {
// 3.執(zhí)行SQL
BOOL isOK = [db executeUpdate:updateSQL,@"王五",@(19),@(3)];
if (isOK) {
NSLog(@"更新數(shù)據(jù)成功");
} else {
NSLog(@"更新數(shù)據(jù)失敗");
}
NSLog(@"影響行數(shù) %d", db.changes);
}];