wcdb github 地址 這里面有詳細(xì)的安裝使用說(shuō)明文檔, 超級(jí)無(wú)敵詳細(xì)那種.
附上我的swfit項(xiàng)目, 項(xiàng)目里面有整個(gè)swift應(yīng)用使用框架, 網(wǎng)絡(luò)請(qǐng)求框架, DSBridge原生與H5交互的用法, 反射知識(shí)的使用, WCDB數(shù)據(jù)庫(kù)的封裝使用, WebRTC音視頻直播demo, socket的使用, socket協(xié)議的封裝使用等等知識(shí)點(diǎn). 希望對(duì)大家有用.-->
swfit完整項(xiàng)目2020持續(xù)更新完善
安裝
使用cocoapod進(jìn)行安裝, 超級(jí)方便
pod 'WCDB.swift'
WCDB支持ORM(直接對(duì)模型對(duì)象進(jìn)行操作), 例如我們要存一個(gè)Sample對(duì)象到數(shù)據(jù)庫(kù):
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 [
//自增主鍵的設(shè)置
.id: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true)
]
}
}
/// 用于定義是否使用自增的方式插入
var isAutoIncrement: Bool = true
/// 用于獲取自增插入后的主鍵值
var lastInsertedRowID: Int64 = 0
}
封裝的數(shù)據(jù)庫(kù)管理工具類(lèi):
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)
}
/// 數(shù)據(jù)庫(kù)與表的初始化
private func createTable() {
do {
//1. 創(chuàng)建主數(shù)據(jù)庫(kù)main的相關(guān)表
try db?.run(transaction: {
createTable(table: DBTableName.sampleTable, modelType: Sample.self)
})
} catch let error {
print("初始化數(shù)據(jù)庫(kù)及ORM對(duì)應(yīng)關(guān)系建立失敗\(error.localizedDescription)")
}
}
///創(chuàng)建表
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)
}
}
///插入數(shù)據(jù)
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
}
///刪除數(shù)據(jù)表
func dropTable(table: DBTableName) -> Void {
do {
try db?.drop(table: table.rawValue)
} catch let error {
debugPrint("drop table error \(error)")
}
}
/// 刪除所有與該數(shù)據(jù)庫(kù)相關(guān)的文件
func removeDbFile() -> Void {
do {
try db?.close(onClosed: {
try db?.removeFiles()
})
} catch let error {
debugPrint("not close db \(error)")
}
}
}
以上是基本的開(kāi)發(fā)使用功能, 如果需要更多更詳細(xì)的封裝使用, 可以參考一下官方文檔[wcdb github 地址](https://github.com/Tencent/wcdb](https://github.com/Tencent/wcdb) 真的超級(jí)詳細(xì).
)