之前开发过的项目涉及到数据库操作都是使用了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;
}
网友评论