iOS · WCDB的使用

weixin_33832340發表於2018-09-20

之前開發過的專案涉及到資料庫操作都是使用了FMDB或者CoreData,直到最近接觸到了使用WCDB的專案。茅舍頓開-資料儲存不用再像FMDB那樣每個業務都去些SQL語句了。當然CoreData大哥也是。但是WCDB是支援iOS、macOS、Android多平臺的移動資料庫框架!

WCDB的特性:

· 易用:WCDB支援一句程式碼即可將資料取出,並且組合成物件
· WINQ(WCDB語言整合查詢):通過WINQ,無需再寫SQL語句
· ORM(Object Relational Mapping):WCDB支援靈活、易用的ORM,可以便捷的定義表、索引、約束、並進行增刪改查操作。
· 高效:效能比FMDB好
· 完整:WCDB滿足了資料庫相關的各種使用場景

直接上重點,如何使用WCDB?
CocoaPods安裝WCDB:

target ‘yourProjectName' do
    platform :ios, ‘8.0'
    pod 'WCDB'
end

(注:WCDB中使用到了C,所以引用它的檔案字尾需要把.m改成.mm)

引入標頭檔案:

 #import <WCDB/WCDB.h>

1、建立資料庫:

***.h
@interface WCBDManage : NSObject
+(instancetype)sharedManager;
@property (strong,nonatomic) WCTDatabase *dataBase;
@end
***.mm
#define kDataBaseFileName @“myWCDB.sqlite"
+(instancetype)sharedManager{
    static WCBDManage *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[WCBDManage alloc] init];
    });
    return manager;
    
}
-(instancetype)init{
    self = [super init];
    if (self) {
        [self createDataBase];
    }
    return self;
}
-(void)createDataBase{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dbPath = [documentPath stringByAppendingString:kDataBaseFileName];
    self.dataBase = [[WCTDatabase alloc] initWithPath:dbPath];
}
@synthesize dataBase = _dataBase;

2、建立model,並對映到資料表中的欄位 (需要遵循 WCTTableCoding 協議)

***.h
#import <Foundation/Foundation.h>
#import <WCDB/WCDB.h>
@interface OrderDBTable : NSObject<WCTTableCoding>
@property (assign,nonatomic) NSInteger orderId;
@property (strong,nonatomic) NSString *orderTitle;
@property (strong,nonatomic) NSString *orderDescribe;
@property (assign,nonatomic) float price;
//需要繫結到表的欄位在這裡宣告,在.mm中繫結
WCDB_PROPERTY(orderId)
WCDB_PROPERTY(orderTitle)
WCDB_PROPERTY(orderDescribe)
WCDB_PROPERTY(price)
***.mm
@implementation OrderDBTable
WCDB_IMPLEMENTATION(OrderDBTable)    //該巨集實現繫結到表
WCDB_SYNTHESIZE(OrderDBTable, orderId)    //該巨集實現繫結到表的欄位
WCDB_SYNTHESIZE(OrderDBTable, orderTitle)
WCDB_SYNTHESIZE(OrderDBTable, orderDescribe)
WCDB_SYNTHESIZE(OrderDBTable, price)

// 約束巨集定義資料庫的主鍵
WCDB_PRIMARY(OrderDBTable, orderId)
// 定義資料庫的索引屬性,它直接定義createTime欄位為索引
// 同時 WCDB 會將表名 + "_index" 作為該索引的名稱
// WCDB_INDEX(OrderDBTable, "_index", createTime)

@end

3、建立表

#define kDataBaseName_orderTable @“kDataBaseName_orderTable"
//建立表
-(void)createOrderTable{
    if (![[LuckyWCBDManage sharedManager].dataBase isTableExists:kDataBaseName_orderTable]) {
        BOOL isCreate = [[LuckyWCBDManage sharedManager].dataBase createTableAndIndexesOfName:kDataBaseName_orderTable withClass:LuckyOrderDBTable.class];
    }
}

4、資料操作

//增加一條資料
+(BOOL)insertObjectWithOrderModel:(OrderDBTable *)model{
    BOOL success = [[WCBDManage sharedManager].dataBase insertObject:model into:kDataBaseName_orderTable];
    return success;
}

//查詢某張表中所有資料
+(NSMutableArray *)searchAllData{
    NSMutableArray *resultArr = [[NSMutableArray alloc] initWithCapacity:0];
    WCTTable *table = [[WCBDManage sharedManager].dataBase getTableOfName:kDataBaseName_orderTable withClass:LuckyOrderDBTable.class];
    NSArray<OrderDBTable *> *arr = [table getAllObjects];
    [resultArr addObjectsFromArray:arr];
    return resultArr;
}

相關文章