WCDB是微信的一个开源数据库框架。
相对于FMDB
来说,WCDB
更快,而且用了ORM
数据-模型绑定,使用更方便,不需要麻烦的sql
语句或者二次封装FMDB
。WCDB
主要是靠WCTColumnCoding
协议实现绑定:
@protocol WCTColumnCoding
@required
+ (instancetype)unarchiveWithWCTValue:(WCTValue *)value; //value could be nil
- (id /* WCTValue* */)archivedWCTValue; //value could be nil
+ (WCTColumnType)columnTypeForWCDB;
@end
- 使用
- 准备:
//创建一个model
@interface Student : NSObject
@property (nonatomic, assign) int studentID;
@property (nonatomic, copy) NSString *name;
//也可以用分类来分层处理
WCDB_PROPERTY(studentID)
WCDB_PROPERTY(name)
@end
@implementation Student
WCDB_IMPLEMENTATION(@"student")
WCDB_SYNTHESIZE(@"student", studentID)
WCDB_SYNTHESIZE(@"student", name)
@end
- 创建:
WCTDatabase *db = [[WCTDatabase alloc] initWithPath:filePath];
if (![db canOpen]) {
[db createTableAndIndexesOfName:@"student" withClass:[Student class]];
}
- 插入:
[db insertOrReplaceObjects:array into: @"student"];
- 删除:
[db deleteObjectsFromTable:@"student" where:Student.studentID.is(studentID)];
[db deleteAllObjectsFromTable:@"student"];
- 查找:
[db getAllObjectsOfClass:[Student class] fromTable: @"student"];
网友评论