WCDB 使用

2021-06-16  本文已影响0人  蓝天白云_Sam

1. 安装:使用Cocoapods安装

pod 'WCDB'

2. 使用

2.1 实现 WCTTableCoding 协议

//Message.h
@interface Message : NSObject

@property int localID;
@property(retain) NSString *content;
@property(retain) NSDate *createTime;
@property(retain) NSDate *modifiedTime;
@property(assign) int unused; //You can only define the properties you need

@end
  1. 在category里遵循WCTTableCoding协议,并定义数据库相关字段
//Message+WCTTableCoding.h
#import "Message.h"
#import <WCDB/WCDB.h>

@interface Message (WCTTableCoding) <WCTTableCoding>

WCDB_PROPERTY(localID)
WCDB_PROPERTY(content)
WCDB_PROPERTY(createTime)
WCDB_PROPERTY(modifiedTime)

@end
  1. 在实现文件里实现相关字段
//Message.mm
#import "Message.h"
@implementation Message

WCDB_IMPLEMENTATION(Message)
WCDB_SYNTHESIZE(Message, localID)
WCDB_SYNTHESIZE(Message, content)
WCDB_SYNTHESIZE(Message, createTime)
WCDB_SYNTHESIZE(Message, modifiedTime)

WCDB_PRIMARY(Message, localID)

WCDB_INDEX(Message, "_index", createTime)

@end

相关宏定义:

除此之外,WCDB还提供了许多可选的宏,用于定义数据库索引、约束等,如:
WCDB_PRIMARY 用于定义主键
WCDB_INDEX 用于定义索引
WCDB_UNIQUE 用于定义唯一约束
WCDB_NOT_NULL 用于定义非空约束

2.2 使用:创建表及增删改查(CRUD)

2.2.1 创建表和索引

定义完成后,只需要调用createTableAndIndexesOfName:withClass:接口,即可创建表和索引。

WCTDatabase *database = [[WCTDatabase alloc] initWithPath:path];
/*
 CREATE TABLE messsage (localID INTEGER PRIMARY KEY,
                        content TEXT,
                        createTime BLOB,
                        modifiedTime BLOB)
 */
BOOL result = [database createTableAndIndexesOfName:@"message"
                                          withClass:Message.class];

2.2.2 插入

//插入
Message *message = [[Message alloc] init];
message.localID = 1;
message.content = @"Hello, WCDB!";
message.createTime = [NSDate date];
message.modifiedTime = [NSDate date];
/*
 INSERT INTO message(localID, content, createTime, modifiedTime) 
 VALUES(1, "Hello, WCDB!", 1496396165, 1496396165);
 */
BOOL result = [database insertObject:message
                                into:@"message"];
//插入
Message *message = [[Message alloc] init];
message.localID = 1;
message.content = @"Hello, WCDB!";
message.createTime = [NSDate date];
message.modifiedTime = [NSDate date];
/*
 INSERT INTO message(localID, content, createTime, modifiedTime) 
 VALUES(1, "Hello, WCDB!", 1496396165, 1496396165);
 */
BOOL result = [database insertOrReplaceObject:message
                                into:@"message"];

2.2.2 删除

//删除
//DELETE FROM message WHERE localID>0;
BOOL result = [database deleteObjectsFromTable:@"message"
                                         where:Message.localID > 0];

2.2.3 修改(更新)

//修改
//UPDATE message SET content="Hello, Wechat!";
Message *message = [[Message alloc] init];
message.content = @"Hello, Wechat!";
BOOL result = [database updateRowsInTable:@"message"
                             onProperties:Message.content
                               withObject:message];

2.2.4 查询

//查询
//SELECT * FROM message ORDER BY localID
NSArray<Message *> *message = [database getObjectsOfClass:Message.class
                                                fromTable:@"message"
                                                  orderBy:Message.localID.order()];

更多关于增删改查的接口,请参考:基础类、CRUD与Transaction

2.2.5 事务

BOOL commited = [database runTransaction:^BOOL {
    [database insertObject:message into:@"message"];
    return YES; //return YES to commit transaction and return NO to rollback transaction.
}];
上一篇下一篇

猜你喜欢

热点阅读