wcdb 开源地址:https://github.com/Tencent/wcdb
一、wcdb介绍
引用官方说法:“WCDB Swift 是一个易用、高效、完整的移动数据库框架,它基于 SQLite 和 SQLCipher 开发。”
鹅厂出品的值得信赖。于是就打算在新的项目中使用它。
三大特性让我们去选择它:
① 易用性
one line of code 是它坚持的原则,大多数操作只需要一行代码即可完成,是不是很赞。
使用WINQ 语句查询,不用为拼接SQL语句而烦恼了,模型绑定映射也是按照规定模板去实现方便快捷。
② 高效性
就拿和fmdb做对比吧:
image
③ 完整性
1、支持基于SQLCipher 加密
2、持全文搜索
3、支持反注入,可以避免第三方从输入框注入 SQL,进行预期之外的恶意操作。
4、用户不用手动管理数据库字段版本,升级方便自动
5、提供数据库修复工具
二、wcdb 安装
可以直接看官方安装教程:https://github.com/Tencent/wcdb/wiki/Swift-%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%BC%E5%AE%B9%E6%80%A7
这里就不过多说明了
三、wcdb 的封装使用
1、模型绑定,直接用wcdb提供的模板
class Sample: TableCodable {var identifier: Int? = nilvar description: String? = nilenum CodingKeys: String, CodingTableKey {typealias Root = Samplestatic let objectRelationalMapping = TableBinding(CodingKeys.self)case identifiercase description}
}
2、数据库创建以及操作单独写了个单例类 HMDataBaseManager.swift
import Foundation
import WCDBSwiftstruct HMDataBasePath {let dbPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true).last! + "/HMDB/HMDB.db"
}class HMDataBaseManager: NSObject {static let share = HMDataBaseManager()let dataBasePath = URL(fileURLWithPath: HMDataBasePath().dbPath)var dataBase: Database?private override init() {super.init()dataBase = createDb()}///创建dbprivate func createDb() -> Database {debugPrint("数据库路径==\(dataBasePath.absoluteString)")return Database(withFileURL: dataBasePath)}///创建表func createTable<T: TableDecodable>(table: String, of ttype:T.Type) -> Void {do {try dataBase?.create(table: table, of:ttype)} catch let error {debugPrint("create table error \(error.localizedDescription)")}}///插入func insertToDb<T: TableEncodable>(objects: [T] ,intoTable table: String) -> Void {do {try dataBase?.insert(objects: objects, intoTable: table)} catch let error {debugPrint(" insert obj error \(error.localizedDescription)")}}///修改func updateToDb<T: TableEncodable>(table: String, on propertys:[PropertyConvertible],with object:T,where condition: Condition? = nil) -> Void{do {try dataBase?.update(table: table, on: propertys, with: object,where: condition)} catch let error {debugPrint(" update obj error \(error.localizedDescription)")}}///删除func deleteFromDb(fromTable: String, where condition: Condition? = nil) -> Void {do {try dataBase?.delete(fromTable: fromTable, where:condition)} catch let error {debugPrint("delete error \(error.localizedDescription)")}}///查询func qureyFromDb<T: TableDecodable>(fromTable: String, cls cName: T.Type, where condition: Condition? = nil, orderBy orderList:[OrderBy]? = nil) -> [T]? {do {let allObjects: [T] = try (dataBase?.getObjects(fromTable: fromTable, where:condition, orderBy:orderList))!debugPrint("\(allObjects)");return allObjects} catch let error {debugPrint("no data find \(error.localizedDescription)")}return nil}///删除数据表func dropTable(table: String) -> Void {do {try dataBase?.drop(table: table)} catch let error {debugPrint("drop table error \(error)")}}/// 删除所有与该数据库相关的文件func removeDbFile() -> Void {do {try dataBase?.close(onClosed: {try dataBase?.removeFiles()})} catch let error {debugPrint("not close db \(error)")}}}
以上的基本满足使用需求。
比较复杂的查询可以使用 prepareSelect 查询接口
///查询所有站点并按字母排序去重func qureyAllStations(cityId: Int) -> [StationModel]{var stationArray = [StationModel]()do {let selectPrep = try HMDataBaseManager.share.dataBase?.prepareSelect(on: StationModel.Properties.all, fromTable: String(describing: StationModel.self)).where(StationModel.Properties.cityid == cityId).group(by: StationModel.Properties.statid).order(by: StationModel.Properties.statpname.asOrder(by: .ascending))stationArray = try selectPrep?.allObjects() ?? []} catch let error {debugPrint("\(error)")}return stationArray}
这里是新项目中最复杂的一个查询了,查询指定城市所有站点,并且去重。
四、最后
要写些复杂的查询语句可以去wiki文档和官方demo的testcase里面查阅,我之前赶项目,没有仔细查找,后面是询问了作者才找到需要的内容。目前只是用了皮毛,在项目后续的使用中继续学习。