1. 程式人生 > >FMDB(資料庫)與Model(資料模型)的結合使用(CRUD)

FMDB(資料庫)與Model(資料模型)的結合使用(CRUD)

一. 宣告

  本文意在探討, 也參考了幾位大神的文章, 在文章最後我會把連結貼出來, 如果有敘述錯誤的地方, 請大神們指正!

二. 前言

  最近在做專案的時候, 我們有一個"我的訊息"模組, 是這樣設計的, 在本地建立一個數據庫, 收到遠端推送的訊息, 將該條訊息存入資料庫, 進入"我的訊息"頁面, 讀取資料庫中的內容, 展示到該頁面, 我們是用的FMDB來進行資料庫的CRUD, 每一條訊息為一個Model, 根據模型的屬性列表來建立資料庫的表, 以model為一個單位來進行資料庫的新增, 刪除, 更新, 查詢. 自己也寫了一個Demo來演示本節要討論的內容.

上Demo: 聯絡人列表, 新增聯絡人, 刪除聯絡人, 搜尋聯絡人, 更新聯絡人資訊

演示視訊地址: http://v.youku.com/v_show/id_XMzkxMTA5NDg1Ng==.html?spm=a2h3j.8428770.3416059.1

Demo地址: https://github.com/RayLeeBoy/LCLFMDBModel

三. 正文

1. 讀取資料庫中的資料

    // 建立資料庫
    [[LCLDataManager shareManager] createDatabaseWithName:@"user"];
    
    // 開啟資料庫
    [[LCLDataManager shareManager] openDatabase];
    
    
// 根據傳入的資料模型來建立表 [[LCLDataManager shareManager] createTableWithName:@"t_contacts" class:[LCLPerson class]]; // 從資料庫中讀取資料 NSMutableArray * mArr = [[LCLDataManager shareManager] queryWithTableName:@"t_contacts"]; // 賦給資料來源 self.dataSource = mArr; // 更新UI [self.tableView reloadData];
// 關閉資料庫 [[LCLDataManager shareManager] closeDatabase];

2. 向資料庫中新增資料

        // 資料庫: 新增資料
        // 建立一條資料
        NSDate * date = [NSDate date];
        NSTimeInterval timeInterval = [date timeIntervalSince1970];
        NSString * userId = [NSString stringWithFormat:@"%.f", timeInterval];
        LCLPerson * person = [LCLPerson new];
        person.userId = userId;
        person.name = @"Ray";
        person.age = @"18";
        person.phone = @"18617673256";
        person.hobby = @"sing song";
        
        // 獲取資料庫
        [[LCLDataManager shareManager] createDatabaseWithName:@"user"];
        
        // 開啟資料庫
        [[LCLDataManager shareManager] openDatabase];
        
        // 獲取表
        [[LCLDataManager shareManager] createTableWithName:@"t_contacts" class:[LCLPerson class]];
        
        // 向資料庫寫入資料
        [[LCLDataManager shareManager] insertWithTableName:@"t_contacts" model:person];
        
        // 關閉資料庫
        [[LCLDataManager shareManager] closeDatabase];

3. 編輯已經存在的資料

        // 資料庫: 更新資料
        // 這個person指的是已經從資料庫中讀取的資料
        LCLPerson * person = nil;
        
        // 更改某個屬性值
        person.age = @"20";
        
        // 獲取資料庫
        [[LCLDataManager shareManager] createDatabaseWithName:@"user"];
        
        // 開啟資料庫
        [[LCLDataManager shareManager] openDatabase];
        
        // 獲取表
        [[LCLDataManager shareManager] createTableWithName:@"t_contacts" class:[LCLPerson class]];
        
        // 更新資料 primaryKey: 表裡的欄位名, primaryValue: 欄位對應的值
        [[LCLDataManager shareManager] updateWithTableName:@"t_contacts" model:person primaryKey:@"userId" primaryValue:person.userId];
        
        // 關閉資料庫
        [[LCLDataManager shareManager] closeDatabase];    

4. 從資料庫中查詢資料

    // 資料庫: 搜尋資料
    // 獲取資料庫
    [[LCLDataManager shareManager] createDatabaseWithName:@"user"];
    
    // 開啟資料庫
    [[LCLDataManager shareManager] openDatabase];
    
    // 獲取表
    [[LCLDataManager shareManager] createTableWithName:@"t_contacts" class:[LCLPerson class]];
    
    // 從資料庫中查詢資料, key: 表中的欄位名, value: 欄位對應的值
    NSMutableArray * mArr = [[LCLDataManager shareManager] queryWithTableName:@"t_contacts" key:@"phone" value:self.searchTextField.text];
    
    // 賦給資料來源
    self.dataSource = mArr;
    
    // 更新UI
    [self.tableView reloadData];
    
    // 關閉資料庫
    [[LCLDataManager shareManager] closeDatabase];

5. 從資料庫中刪除資料

        // 資料庫: 刪除資料
        // 獲取資料庫
        [[LCLDataManager shareManager] createDatabaseWithName:@"user"];

        // 開啟資料庫
        [[LCLDataManager shareManager] openDatabase];

        // 要刪除的資料
        LCLPerson * person = self.dataSource[indexPath.row];
        
        // 從資料庫中把這條資料刪除, key: 表中的欄位, value: 欄位對應的值
        [[LCLDataManager shareManager] removeWithTableName:@"t_contacts" key:@"userId" value:person.userId];

        // 關閉資料庫
        [[LCLDataManager shareManager] closeDatabase];

        // 更新資料來源
        [self.dataSource removeObjectAtIndex:indexPath.row];
        
        // 更新UI
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

四. 參考文章

  1. FMDB使用方法

  2. iOS-FMDB資料庫之增刪改查使用

五. 總結

  Demo演示放在了youku, Demo程式碼放在了github, 大家可以下載看完整的程式碼及實現邏輯, 有不足的地方, 或者大家有更好的想法, 歡迎來此討論!

Talk is cheap. Show me the code.