wcdb github 地址 这里面有详细的安装使用说明文档, 超级无敌详细那种.
附上我的swfit项目, 项目里面有整个swift应用使用框架, 网络请求框架, DSBridge原生与H5交互的用法, 反射知识的使用, WCDB数据库的封装使用, WebRTC音视频直播demo, socket的使用, socket协议的封装使用等等知识点. 希望对大家有用.-->
swfit完整项目2020持续更新完善
安装
使用cocoapod进行安装, 超级方便
pod 'WCDB.swift'
WCDB支持ORM(直接对模型对象进行操作), 例如我们要存一个Sample对象到数据库:
import WCDBSwift
class Sample: NSObject,TableCodable {
var id: Int = 0
var ssid: String?
var password: String?
enum CodingKeys: String, CodingTableKey {
typealias Root = Sample
static let objectRelationalMapping = TableBinding(CodingKeys.self)
case id
case ssid
case password
//Column constraints for primary key, unique, not null, default value and so on. It is optional.
static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
return [
//自增主键的设置
.id: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true)
]
}
}
/// 用于定义是否使用自增的方式插入
var isAutoIncrement: Bool = true
/// 用于获取自增插入后的主键值
var lastInsertedRowID: Int64 = 0
}
封装的数据库管理工具类:
import WCDBSwift
struct WcdbDataPath {
static let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/DB/wcdb.db"
}
enum DBTableName : String {
case sampleTable = "Sample"
}
class DBmanager: NSObject {
static let share = DBmanager.init()
var db: Database?
override init() {
super.init()
db = createDB()
createTable()
}
private func createDB() -> Database {
return Database(withPath: WcdbDataPath.basePath)
}
/// 数据库与表的初始化
private func createTable() {
do {
//1. 创建主数据库main的相关表
try db?.run(transaction: {
createTable(table: DBTableName.sampleTable, modelType: Sample.self)
})
} catch let error {
print("初始化数据库及ORM对应关系建立失败\(error.localizedDescription)")
}
}
///创建表
private func createTable<T: TableDecodable>(table: DBTableName, modelType: T.Type) {
do {
try db?.create(table: table.rawValue, of: modelType)
}catch let error {
debugPrint(error.localizedDescription)
}
}
///插入数据
public func inser<T: TableEncodable>(objects:[T], intoTable table: DBTableName){
do {
try db?.insert(objects: objects, intoTable: table.rawValue)
}catch let error {
debugPrint(error.localizedDescription)
}
}
///修改
public func update<T: TableEncodable>(fromTable table: DBTableName, on propertys:[PropertyConvertible], itemModel object:T,where condition: Condition? = nil){
do {
try db?.update(table: table.rawValue, on: propertys, with: object, where: condition)
} catch let error {
debugPrint(" update obj error \(error.localizedDescription)")
}
}
///删除
public func deleteFromDb(fromTable table: DBTableName, where condition: Condition? = nil){
do {
try db?.delete(fromTable: table.rawValue, where:condition)
} catch let error {
debugPrint("delete error \(error.localizedDescription)")
}
}
///查询
public func qurey<T: TableDecodable>(fromTable table: DBTableName, where condition: Condition? = nil, orderBy orderList:[OrderBy]? = nil) -> [T]? {
do {
let allObjects: [T] = try (db?.getObjects(fromTable: table.rawValue, where:condition, orderBy:orderList))!
debugPrint("\(allObjects)");
return allObjects
} catch let error {
debugPrint("no data find \(error.localizedDescription)")
}
return nil
}
///删除数据表
func dropTable(table: DBTableName) -> Void {
do {
try db?.drop(table: table.rawValue)
} catch let error {
debugPrint("drop table error \(error)")
}
}
/// 删除所有与该数据库相关的文件
func removeDbFile() -> Void {
do {
try db?.close(onClosed: {
try db?.removeFiles()
})
} catch let error {
debugPrint("not close db \(error)")
}
}
}
以上是基本的开发使用功能, 如果需要更多更详细的封装使用, 可以参考一下官方文档[wcdb github 地址](https://github.com/Tencent/wcdb](https://github.com/Tencent/wcdb) 真的超级详细.
)