什么是FMDB
最近在實作一個簡單APP的收藏功能,需要用到資料庫,將資料存盤到本地,但在iOS中使用C語言函式對原生SQLite資料庫進行增刪改查操作,是比較麻煩的,FMDB是一個針對libsqlite3框架進行封裝的第三方庫,它使用OC封裝了c語言的API,使用起來比較方便,
FMDB的主要型別
FMDatabase:一個FMDatabase物件代表一個單獨的SQLite資料庫,通過SQLite陳述句執行資料庫的增刪改查操作
FMResultSet:使用FMDatabase物件查詢資料庫后的結果集
FMDatabaseQueue:用于多執行緒操作資料庫,它保證執行緒安全
如何使用FMDB
匯入庫
和其他的第三庫一樣,我們需要將FMDB匯入到工程,這里我使用的是CocoaPod匯入,這個在之前的博客中有介紹過如何匯入,iOS——Masonry的簡單使用,只需要將其中的pod 'Masonry',改成pod 'FMDB'即可,
如果要使用多個第三方庫,直接加入pod ‘FMDB’,
platform:ios,'9.0'
target '知乎日報' do
pod 'JSONModel'
pod 'Masonry'
pod 'SDWebImage'
pod 'MJRefresh'
pod 'FMDB'
end
最后cd 到檔案目錄下,輸入 pod install.這樣就匯入了,
簡單步驟
1.首先匯入頭檔案
#import "FMDB.h"
2.創建資料庫屬性,以及你想匯入資料庫的屬性
@interface HomeController ()
@property (nonatomic, strong) FMDatabase *db;
// 資料庫路徑
@property (nonatomic, strong) NSString* dbPath;
// 設定資料庫存盤的資料
@property (nonatomic, strong) NSString* titleString;
@property (nonatomic, strong) NSString* imageUrlString;
@property (nonatomic, strong) NSString* webViewString;
// 標記資料是否被找到
@property (nonatomic, assign) int ans;
@end
3.創建資料庫
// 創建資料庫
- (void)getDatabase {
//獲得資料庫檔案的路徑
NSString* doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* fileName=[doc stringByAppendingPathComponent:@"HotDog.sqlite"];
self.dbPath = fileName;
// 獲取資料庫
FMDatabase* testDatebase = [FMDatabase databaseWithPath:self.dbPath];
// 打開資料庫
if ([testDatebase open]) {
// 創表
// 表中名字是你存盤資料的名字
// 下面的表存盤了收藏界面的文字資料,圖片的下載地址,以及webView的下載地址
BOOL result = [testDatebase executeUpdate:@"CREATE TABLE IF NOT EXISTS t_agreeOrder (id integer PRIMARY KEY AUTOINCREMENT, titleString text NOT NULL, imageUrlString text NOT NULL, webString text NOT NULL);"];
if (result) {
NSLog(@"創表成功");
} else {
NSLog(@"創表失敗");
}
}
self.db = testDatebase;
}
[doc stringByAppendingPathComponent:@“HotDog.sqlite”] 后面的字串就是資料庫的名字,當這個資料庫不存在是,系統會自動創建,如果存在時,那么下次就可以根據這個名字打開這個資料庫
這里有一個創建表的操作,個人理解為在資料中規定資料型別,相當于一個索引,可以根據這個名字去訪問這個型別的資料,
下面就可以進行對表的一些操作了,
增刪查改
這里是我在專案中的一些應用,使用背景:點擊收藏按鈕,如果資料不在資料庫,那么插入資料庫,如果資料以及在資料庫,那么相當于取消收藏,就去洗掉資料庫中的資料,
1.首先根據滾動視圖的位置更新需要插入的屬性,
// 更新收藏資料
- (void) repalceData:(UIScrollView *)scrollView {
int page = scrollView.contentOffset.x / WIDTH;
_titleString = [_titleArray[page] title];
StoriesModel *story = _titleArray[page];
_imageUrlString = story.images[0];
_webViewString = _webViewArray[page];
// NSLog(@"%@ %@ 777%@", _titleString, _imageUrlString, _webViewString);
}
2.查詢資料庫中是否存在資料
// 查詢資料
- (void) queryData {
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
// 1.執行查詢陳述句
FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_agreeOrder"];
_ans = 0;
// 2.遍歷結果
while ([resultSet next]) {
NSString* testString = [resultSet stringForColumn:@"titleString"];
NSLog(@"%@11", testString);
if ([testString isEqual:_titleString]) {
_ans = 1;
break;
}
}
[db close];
}
}
NSString* testString = [resultSet stringForColumn:@"titleString"];這句話就是根據我們當時創表的索引名去獲得對應的資料,
3.插入資料
//插入資料
-(void)insert {
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
// 向表中插入符合要求的資料
BOOL res = [self.db executeUpdate:@"INSERT INTO t_agreeOrder (titleString, imageUrlString, webString) VALUES (?, ?, ?);", _titleString, _imageUrlString, _webViewString];
if (!res) {
NSLog(@"增加資料失敗");
} else{
NSLog(@"增加資料成功");
}
[db close];
}
}
[self.db executeUpdate:@"INSERT INTO t_agreeOrder (titleString, imageUrlString, webString) VALUES (?, ?, ?);", _titleString, _imageUrlString, _webViewString];: 這里的括號中的對應的為表中的索引,插入的值就是我們后面定義的一些屬性,
4.洗掉資料
// 洗掉資料
- (void)deleteData {
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"delete from 't_agreeOrder' where titleString = ?";
BOOL res = [self.db executeUpdate:sql, _titleString];
NSString *secondSql = @"delete from 't_agreeOrder' where imageUrlString = ?";
BOOL res1 = [self.db executeUpdate:secondSql, _imageUrlString];
NSString *thirdSql = @"delete from 't_agreeOrder' where webString = ?";
BOOL res2 = [self.db executeUpdate:thirdSql, _webViewString];
if (!res || !res1 || !res2) {
NSLog(@"資料洗掉失敗");
} else {
NSLog(@"資料洗掉成功");
}
[db close];
}
}
NSString *sql = @"delete from 't_agreeOrder' where titleString = ?"; BOOL res = [self.db executeUpdate:sql, _titleString];: 當where titleString = ?條件成立時,表中指定元素,
5.更新資料
// 更新資料
- (void)updateData {
FMDatabase *db = [FMDatabase databaseWithPath:self.dbPath];
if ([db open]) {
NSString *sql = @"UPDATE t_agreeOrder SET id = ? WHERE titleString = ?";
BOOL res = [db executeUpdate:sql, @"1", @"修改資料"];
if (!res) {
NSLog(@"資料修改失敗");
} else {
NSLog(@"資料修改失敗");
[self queryData];
}
[db close];
}
}
@"UPDATE t_agreeOrder SET id = ? WHERE titleString = ?":id是這型別別的索引,當后面的條件成立時修改,
簡單就了解了這么一部分,后面再次使用或者深入學習時,繼續補充,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357148.html
標籤:其他
上一篇:HOOK安卓so的至少5個函式
