使用FMDB保存首頁數(shù)據(jù)

FD87DC16F4A42922C5DFDCCF96332A13.jpg

很多的APP都在沒有網(wǎng)絡的時候會保存上一次加載的數(shù)據(jù),他們最常用的就是使用SQLite數(shù)據(jù)庫,其中FMDB也是封裝比較好的框架之一,下面來分享一下FMDB的使用!

  1. 下載并導入框架就沒什么好說的了
  2. 創(chuàng)建一個管理的工具類(SQLiteManager)繼承NSObject
    SQLiteManager.m 里面的內(nèi)容如下,我寫的是類方法,你也可以寫對象方法,在弄一個單例什么的,寫類方法主要是為了方便!

說一下項目的背景 :我們首頁數(shù)據(jù)返回來的是一個json,通過框架直接轉成了字典,領導要求只保存第一頁數(shù)據(jù)就可以,所以我沒有把所有的數(shù)據(jù)按照字段去解析存儲,我是直接存儲的json數(shù)據(jù),也省去了以后的項目開發(fā)中增加新的字段,需要對數(shù)據(jù)庫升級的麻煩只能說我比較懶廢話說了這么多下面是代碼!!!

/// 更新數(shù)據(jù)
///
/// @param data 數(shù)據(jù)
/// @param page 頁碼
+ (void)updateHomeData:(NSString *)data page:(NSUInteger)page;

/// 插入數(shù)據(jù)
///
/// @param data 數(shù)據(jù)
/// @param page 頁碼
+ (void)insertHomeData:(NSString *)data page:(NSUInteger)page;

/// 查詢數(shù)據(jù)
+ (NSArray *)queryHomeData;

/// 刪除數(shù)據(jù)
///
/// @param page 頁碼
+ (void)deleteHomeDataWithPage:(NSInteger)page;

3.在SQLiteManager.h文件中代碼如下:

//定義一個全局對象
static FMDatabase *_db;

//該方法會在調用該類中方法的時候先調用
+ (void)initialize{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"homeData.db"];

//1.創(chuàng)建庫
_db = [FMDatabase databaseWithPath:dbFilePath];

//2.打開連接
BOOL result = [_db open];

if (result) {
    //3.創(chuàng)建表
    /**
     如果是創(chuàng)建表,新增數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù),我們都一個方法
     executeUpdate
     如果是查詢executeQuery
     */
    BOOL result2 = [_db executeUpdate:@"create table if not exists t_home(id integer primary key,data text not null,page integer not null);"];
    if (result2) {
        NSLog(@"創(chuàng)建表成功!!!");
    }
}

}

//添加數(shù)據(jù)
+ (void)insertHomeData:(NSString *)data page:(NSUInteger)page{
[_db executeUpdateWithFormat:@"insert into t_home(data,page) values(%@,%ld)",data,page];
}

//更新數(shù)據(jù)
+ (void)updateHomeData:(NSString *)data page:(NSUInteger)page{
[_db executeUpdateWithFormat:@"update t_home set data=%@,page = %ld",data,page];
}

//刪除數(shù)據(jù)
+ (void)deleteHomeDataWithPage:(NSInteger)page{
[_db executeUpdateWithFormat:@"delete from t_home where page = %ld",page];
}
//查詢數(shù)據(jù)
+ (NSArray *)queryHomeData{

FMResultSet *resultSet = [_db executeQuery:[NSString stringWithFormat:@"select * from t_home"]];
// 如果你想查詢某個字段可以這么寫(注意%號的個數(shù))
//    FMResultSet *resultSet = [_db executeQuery:[NSString stringWithFormat:@"select * from t_home where name LIKE '%%%@%%'",zhangsan]];
NSMutableArray *homeDatas = [NSMutableArray array];

while (resultSet.next) {
    NSString *homeData = [resultSet stringForColumn:@"data"];
// 將字符串轉換成對象(下面會說為什么存字符串!!!)
    NSData *data = [homeData dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
    //將其添加到數(shù)組中
    [homeDatas addObject:array];
}

return homeDatas.copy;
}

4.使用FMDB(重點來了!!!)
在請求數(shù)據(jù)之前先加載數(shù)據(jù)庫中的數(shù)據(jù)

// 取出數(shù)據(jù)庫中的數(shù)據(jù),判斷是否有數(shù)據(jù)
NSArray *array = [SQLiteManager queryHomeData];
    if (array && array.count > 0) {
// 字典轉模型
    InformationReturnData * returnData = [InformationReturnData mj_objectWithKeyValues:array.firstObject];
    self.listDataArray = returnData.data;
    [self.tableView reloadData];
}
else
{
    // 沒有重數(shù)據(jù)庫中加載到數(shù)據(jù),向服務器請求數(shù)據(jù)
    [self requestDataWithPage:self.page];
}

5.下面是寫入數(shù)據(jù)到數(shù)據(jù)庫(肯定是在數(shù)據(jù)請求回來之后了~廢話!)

NSError *err = nil;
// 此處是將對象轉成json字符串寫入到數(shù)據(jù)庫中(數(shù)據(jù)庫里不能存對象,你不會不知道吧~)
NSData *data = [NSJSONSerialization dataWithJSONObject:jsonData options:NSJSONWritingPrettyPrinted error:&err];
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSInteger page = returnData.page;
// 刪除之前的舊數(shù)據(jù)
[SQLiteManager deleteHomeDataWithPage:page];
// 保存新的數(shù)據(jù)
[SQLiteManager insertHomeData:jsonStr page:page];

總結:這個是我重項目里分離出來的,就沒有demo了,歡迎大家點贊和評論,有不懂的直接留言,我也不一定會哈!

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,155評論 25 708
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,422評論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評論 19 139
  • 1、 前段時間,由于跟男朋友剛分別的愁苦加上裸辭后對未來不確定的恐慌,讓我躲在出租屋里暗無天日,每次夜幕降臨都會難...
    阿毛mao閱讀 506評論 0 0
  • 我是一名超級熱愛東方舞的舞蹈老師,我從小就喜歡各種舞蹈,喜歡做一名舞蹈家,長大了省吃檢用賺的錢都去學舞蹈了,我20...
    青青蝶舞閱讀 891評論 0 0

友情鏈接更多精彩內(nèi)容