本文旨在记录自己项目过程中的一些使用,想要了解更多的小伙伴移步到这里:WCDB wiki
使用cocoapods集成到项目中
pod 'WCDB'
这里封装了一个工具类,专门对数据库创建,表创建,查询,更新等操作的封装!
由于WCDB是基于Objective-C++,因此需要将引用WCDB的源文件后缀.m改为.mm
为避免大范围更改,我们将使用WCDB代码的类隔绝起来!
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^WCDBblock)(BOOL isSuccess,id result);
@interface WMDBManager : NSObject
@property (nonatomic, copy)WCDBblock block;
+ (instancetype)shareSingle;
/**
* 创建数据库的操作
* @param name 数据库名称
* @param block 数据库创建成功与否的回调
*/
- (void)initWCDBWithName:(NSString *)name block:(WCDBblock)block;
/**
*创建表
* @param name 表名
* @param className 类名
*/
- (void)createTablesWithTableName:(NSString *)name ClassName:(NSString *)className;
@end
#import "WMDBManager.h"
#import "WMXXXModel+WCTTableCoding.h"
@interface WMDBManager()
{
WCTDatabase * database;
}
@end
@implementation WMDBManager
+ (WMDBManager *)shareSingle {
static WMDBManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (manager == nil) {
manager = [[WMDBManager alloc] init];
}
});
return manager;
}
- (NSString *)getWCDBPathWithName:(NSString *)name {
// 获取 Document 文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
// 路径
NSString *documentsDirectory = [paths firstObject];
// 获取数据库路径 name:数据库名称
NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:name];
// 判断数据库是否在目录下,如果没有就创建(拷贝)进来
if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
NSLog(@"文件已经存在了");
}else {
//本地初始化是的数据库文件,拷贝到创建的WCDB数据库
NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle] pathForResource:"文件名" ofType:nil];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
[[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath contents:mainBundleFile attributes:nil];
}
// 获取路径
return documentLibraryFolderPath;
}
/** 创建数据库
*/
- (void)initWCDBWithName:(NSString *)name block:(WCDBblock)block {
//根据获取的路径创建数据库
database = [[WCTDatabase alloc] initWithPath:[self getWCDBPathWithName:name]];
//测试数据库是否能够打开
if ([database canOpen]) {
block(YES,@"创建数据库成功");
//WMXXXModel 模型类,根据模型字段映射表字段
[self createTablesWithTableName:@"表名" ClassName:@"WMXXXModel"];
} else {
block(NO,@"创建数据库失败");
}
}
- (void)createTablesWithTableName:(NSString *)name ClassName:(NSString *)className {
//根据类名获取类
Class objc = NSClassFromString(className);
//wcdb创建表的方法
BOOL result = [database createTableAndIndexesOfName:name withClass:objc.class];
NSLog(@"%@建表%@",name,result ? @"成功":@"失败" );
}
WCDB基于WINQ,引入了Objective-C++代码,因此对于所有引入WCDB的源文件,都需要将其后缀.m改为.mm。为减少影响范围,可以通过Objective-C的category特性将其隔离,达到只在model层使用Objective-C++编译,而不影响controller和view。创建的Objective-C的category需要遵循WCTTableCoding协议!为了简便,WCDB提供了文件模版!Xcode设置及创建详见WCDB ORM使用教程
与正常创建模型文件的.h一样
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface WMXXXModel : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *height;
@property (nonatomic, strong) NSString *idCard;
@end
NS_ASSUME_NONNULL_END
#import "WMXXXModel.h"
#import "WMXXXModel+WCTTableCoding.h"
@implementation WMXXXModel
WCDB_IMPLEMENTATION(WMXXXModel)
WCDB_SYNTHESIZE(WMXXXModel, name)
WCDB_SYNTHESIZE(WMXXXModel, age)
WCDB_SYNTHESIZE(WMXXXModel, height)
WCDB_SYNTHESIZE(WMXXXModel, idCard)
@end
//注:由于WCDB_SYNTHESIZE(className, propertyName)宏默认使用propertyName作为字段名,
因此在修改propertyName后,会导致错误,
需用WCDB_SYNTHESIZE_COLUMN(className, newPropertyName, "oldPropertyName")重新映射。
#import "WMXXXModel.h"
#import <WCDB/WCDB.h>
@interface WMXXXModel (WCTTableCoding) <WCTTableCoding>
WCDB_PROPERTY(name)
WCDB_PROPERTY(age)
WCDB_PROPERTY(height)
WCDB_PROPERTY(idCard)
@end
WMXXXModel *xxModel = [[WMXXXModel alloc] init];
xxModel.name = @"小明";
xxModel.age = @"18";
xxModel.height = @"176";
xxModel.idCard = @"中国";
/*
INSERT INTO "表名" (name, age, height, idCard) VALUES("小明", "18", "176", "中国");
*/
BOOL result = [database insertObject:xxModel into:@"表名"];
//DELETE FROM "表名" WHERE idCard == *******11;
BOOL result = [database deleteObjectsFromTable:@"表名" where:WMXXXModel.idCard == *******11];
//UPDATE "表名" SET age="18";
WMXXXModel *xxModel = [[WMXXXModel alloc] init];
xxModel.age = @"28";
BOOL result = [database updateRowsInTable:@"表名" onProperties:WMXXXModel.age withObject:xxModel];
//SELECT * FROM "表名" ORDER BY age
NSArray<WMXXXModel *> *xxModel = [database getObjectsOfClass:WMXXXModel.class fromTable:@"表名" orderBy:WMXXXModel.age.order()];
更多的使用请查看WCDB 基础类、CRUD与Transaction