FMDB是一個(gè)輕量級(jí)的數(shù)據(jù)庫,用于將網(wǎng)絡(luò)資源存儲(chǔ)在本地。FMDB 將 SQLite API 進(jìn)行了很友好的封裝,使用上非常方便,對(duì)于那些使用純 Sqlite API 來進(jìn)行數(shù)據(jù)庫操作的 app,可以考慮將其遷移到基于 FMDB 上,這對(duì)于以后數(shù)據(jù)庫相關(guān)功能的開發(fā)維護(hù),可以提高不少效率。
成都創(chuàng)新互聯(lián)公司成都網(wǎng)站建設(shè)按需網(wǎng)站建設(shè),是成都網(wǎng)站建設(shè)公司,為OPP膠袋提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開發(fā)等。成都網(wǎng)站營銷推廣熱線:13518219792
什么是FMDB
FMDB的優(yōu)點(diǎn)
FMDB有三個(gè)主要的核心類:
1、FMDatabase
一個(gè)FMDatabase對(duì)象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫
用來執(zhí)行SQL語句
2、FMResultSet
使用FMDatabase執(zhí)行查詢后的結(jié)果集
3、FMDatabaseQueue
用于在多線程中執(zhí)行多個(gè)查詢或更新,它是線程安全的
path文件路徑有三種情況:
1、具體文件路徑
如果不存在會(huì)自動(dòng)創(chuàng)建
2、空字符串@“”
會(huì)在臨時(shí)目錄創(chuàng)建一個(gè)空的數(shù)據(jù)庫
當(dāng)FMDatabase連接關(guān)閉時(shí),數(shù)據(jù)庫文件也被刪除
3、nil
會(huì)創(chuàng)建一個(gè)內(nèi)存中臨時(shí)數(shù)據(jù)庫,當(dāng)FMDatabase連接關(guān)閉時(shí),數(shù)據(jù)庫會(huì)被銷毀
一. 將FMDB第三方庫引入到項(xiàng)目
將 FMDB down下來,然后copy 到你的工程中。
注意:需要 libsqlite3.dylib 依賴(具體流程 Project->TARGETS->Build Phases->Link Binary With Libraries,然后點(diǎn)擊+號(hào)引用該動(dòng)態(tài)庫)
二. 數(shù)據(jù)庫的操作:增刪改查
通常對(duì)數(shù)據(jù)庫的操作,我們一般稱為CURD,即對(duì)表進(jìn)行創(chuàng)建(Create)、更新(Update)、讀取(Read)和刪除(Delete)操作。
基于FMDB的添刪改查操作, 其中添加刪除更改的操作都非常簡(jiǎn)單,不需要做太多操作,只需要用到FMDB封裝好的executeUpdate方法就行了。
對(duì)數(shù)據(jù)庫中存儲(chǔ)的每一個(gè)值都有一個(gè)類型
1. NULL 這個(gè)值為空值
2. INTEGER 值被標(biāo)識(shí)為整數(shù),依據(jù)值的大小可以依次被存儲(chǔ)1~8個(gè)字節(jié)
3. REAL 所有值都是浮動(dòng)的數(shù)值
4. TEXT 值為文本字符串
5. BLOB 值為blob數(shù)據(jù)
這里寫一個(gè) Demo, 測(cè)試一下!
#import "FMDatabase.h" #import "FMDatabaseQueue.h"
獲取數(shù)據(jù)庫文件的路徑:
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [doc stringByAppendingPathComponent:@"user.sqlite"]; self.dbPath = path; NSLog(@"dbPath---%@", path);
建表:
// 建表 - (void)createTable { NSLog(@"%s", __func__); NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:self.dbPath] == NO) { // create it FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) { NSString *sql = @"CREATE TABLE 'User' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' VARCHAR(30), 'password' VARCHAR(30))"; BOOL res = [db executeUpdate:sql]; if (!res) { NSLog(@"error when creating db table"); } else { NSLog(@"success to creating db table"); } [db close]; } else { NSLog(@"error when open db"); } } }
插入數(shù)據(jù):
// 插入數(shù)據(jù) - (void)insertData { NSLog(@"%s", __func__); static int idx = 1; FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) { NSString *sql = @"insert into user (name, password) values(?, ?) "; NSString *name = [NSString stringWithFormat:@"ZL%d", idx++]; BOOL res = [db executeUpdate:sql, name, @"girl"]; if (!res) { NSLog(@"error to insert data"); } else { NSLog(@"success to insert data"); ZLTestModel *model = [ZLTestModel modelWith:name id:idx]; [self.userArr addObject:model]; [self.tableView reloadData]; } [db close]; } }
更新數(shù)據(jù):
// 更新數(shù)據(jù) - (void)updateData { NSLog(@"%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) { NSString *sql = @"UPDATE USER SET id = ? WHERE name = ?"; BOOL res = [db executeUpdate:sql, @"1", @"zl"]; if (!res) { NSLog(@"error to UPDATE data"); } else { NSLog(@"success to UPDATE data"); [self queryData]; } [db close]; } }
刪除數(shù)據(jù):
// 刪除數(shù)據(jù) - (void)deleteData { NSLog(@"%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) { NSString *sql = @"delete from user"; BOOL res = [db executeUpdate:sql]; if (!res) { NSLog(@"error to delete db data"); } else { NSLog(@"success to delete db data"); [self.userArr removeAllObjects]; [self.tableView reloadData]; } [db close]; } }
查詢數(shù)據(jù):
// 查詢數(shù)據(jù) - (void)queryData { NSLog(@"%s", __func__); FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath]; if ([db open]) { NSString *sql = @"select *from user"; FMResultSet *rs = [db executeQuery:sql]; while ([rs next]) { int userId = [rs intForColumn:@"id"]; NSString *name = [rs stringForColumn:@"name"]; NSString *pass = [rs stringForColumn:@"password"]; NSLog(@"user id = %d, name = %@, pass = %@", userId, name, pass); ZLTestModel *model = [ZLTestModel modelWith:name id:userId]; [self.userArr addObject:model]; [self.tableView reloadData]; } [db close]; } }
multithread:
- (void)multithread { NSLog(@"%s", __func__); FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath]; dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL); dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL); dispatch_async(q1, ^{ for (int i = 0; i < 100; ++i) { [queue inDatabase:^(FMDatabase *db) { NSString *sql = @"insert into user (name, password) values(?, ?) "; NSString *name = [NSString stringWithFormat:@"queue111 %d", i]; BOOL res = [db executeUpdate:sql, name, @"boy"]; if (!res) { NSLog(@"error to add db data: %@", name); } else { NSLog(@"success to add db data: %@", name); } }]; } }); dispatch_async(q2, ^{ for (int i = 0; i < 100; ++i) { [queue inDatabase:^(FMDatabase *db) { NSString *sql = @"insert into user (name, password) values(?, ?) "; NSString *name = [NSString stringWithFormat:@"queue222 %d", i]; BOOL res = [db executeUpdate:sql, name, @"boy"]; if (!res) { NSLog(@"error to add db data: %@", name); } else { NSLog(@"success to add db data: %@", name); } }]; } }); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。