與sqlite比較
本質(zhì):sqlite币呵。SwiftSqlite是swift的封裝怀愧。類似FMDB是OC的封裝侨颈。
易于使用原因:sql語(yǔ)句轉(zhuǎn)化為對(duì)應(yīng)關(guān)于對(duì)象操作。其實(shí)對(duì)應(yīng)的是相應(yīng)的sql語(yǔ)句芯义。
使用
初始化
- 數(shù)據(jù)庫(kù)哈垢,表,表結(jié)構(gòu)扛拨,初始化
public static let standard = PTDownloadTable()
//數(shù)據(jù)庫(kù)
private var db: Connection?
//表
private let downLoadFile = Table("downLoadFile")
// 列(數(shù)據(jù)庫(kù)表結(jié)構(gòu))
private let id = Expression<Int64>("id")
private let fielUrl = Expression<String>("fielUrl")
private let downLoadState = Expression<String>("downLoadState")
private let isBrowse = Expression<Bool>("isBrowse")
private let destinationFielUrl = Expression<String>("destinationFielUrl")
private let downLoadTime = Expression<String>("downLoadTime")
private let type = Expression<String>("type")
private let title = Expression<String>("title")
private let brief = Expression<String>("biref")
init () {
//
self.db = createDB()
createTable()
}
- 創(chuàng)建數(shù)據(jù)庫(kù)并連接
/// 創(chuàng)建數(shù)據(jù)庫(kù),并連接
private func createDB() -> Connection? {
//獲取doc路徑
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
//如果不存在的話耘分,創(chuàng)建一個(gè)名為db.sqlite3的數(shù)據(jù)庫(kù),并且連接數(shù)據(jù)庫(kù)
do {
let db = try Connection("\(path)/db.sqlite3")
print("?數(shù)據(jù)庫(kù)創(chuàng)建并連接成功:\(db)")
return db
} catch {
print("??數(shù)據(jù)庫(kù)創(chuàng)建并連接失敗:\(error)")
return nil
}
}
- 創(chuàng)建表
/// 創(chuàng)建下載表,已經(jīng)存在不會(huì)再次創(chuàng)建
private func createTable() {
if let db = self.db {
do {
try db.run(downLoadFile.create(block: { (t) in
t.column(id, primaryKey: .autoincrement)
t.column(fielUrl, defaultValue: "")
t.column(downLoadState, defaultValue: "0")
t.column(isBrowse, defaultValue: false)
t.column(destinationFielUrl, defaultValue: "")
t.column(downLoadTime, defaultValue: "")
t.column(type, defaultValue: "")
t.column(title, defaultValue: "")
t.column(brief, defaultValue: "")
}))
print("?下載列表創(chuàng)建成功")
} catch {
print("??下載列表創(chuàng)建失敗:\(error)")
}
}
}
注意: 第一次創(chuàng)建成功绑警,之后不會(huì)再次創(chuàng)建求泰。
增
/// 增加記錄
public func insertOne(_ fileModel: PTDownLoadModel) {
if let db = self.db {
let insertDownLoadModel = downLoadFile.insert(fielUrl <- fileModel.fielUrl,
downLoadState <- fileModel.downLoadState,
isBrowse <- fileModel.isBrowse,
destinationFielUrl <- fileModel.destinationFielUrl,
downLoadTime <- fileModel.downLoadTime)
do {
try db.run(insertDownLoadModel)
print("?增加成功")
} catch {
print("??增加失敗:\(error)")
}
}
}
查
/// 查詢多條記錄
public func queryManyRecord() -> [PTDownLoadModel]? {
var array: [PTDownLoadModel] = []
if let db = self.db {
do {
for fileModel in try db.prepare(downLoadFile) {
let downLoadModel = PTDownLoadModel()
downLoadModel.fielUrl = fileModel[fielUrl]
downLoadModel.downLoadState = fileModel[downLoadState]
downLoadModel.isBrowse = fileModel[isBrowse]
downLoadModel.destinationFielUrl = fileModel[destinationFielUrl]
downLoadModel.downLoadTime = fileModel[downLoadTime]
downLoadModel.type = fileModel[type]
downLoadModel.title = fileModel[title]
downLoadModel.brief = fileModel[brief]
array.append(downLoadModel)
}
print("?查詢多條數(shù)據(jù)成功")
return array
} catch {
print("??查詢多條數(shù)據(jù)失敗:\(error)")
return nil
}
}
return nil
}
/// 查詢單條記錄
public func queryOneRecord(_ url: String) -> Bool {
if let db = self.db {
do {
for fileModel in try db.prepare(downLoadFile) {
if url == fileModel[fielUrl] {
print("?查詢單條數(shù)據(jù)成功:\(url)")
return true
}
}
print("??查詢單條數(shù)據(jù)失敗")
return false
} catch {
print("??查詢單條數(shù)據(jù)失敗:\(error)")
return false
}
}
return false
}
改
///更新單條條記錄:下載狀態(tài)和下載地址
public func updateOneLoadRecord(_ url: String,_ state: String,_ destinationUrl: String,_ loadTime: String,_ typeStr: String,_ titleStr: String,_ briefStr: String) {
if let db = self.db {
let fileModel = downLoadFile.filter(fielUrl == url)
do {
if destinationUrl == "" {
try db.run(fileModel.update(downLoadState <- state))
} else if loadTime == "" {
try db.run(fileModel.update(downLoadState <- state))
} else {
//只有真正下載完成,才更新:下載地址和下載時(shí)間
try db.run(fileModel.update(downLoadState <- state,
destinationFielUrl <- destinationUrl,
downLoadTime <- loadTime,
type <- typeStr,
title <- titleStr,
brief <- briefStr))
}
print("?更新單條條記錄(下載狀態(tài)和下載地址)數(shù)據(jù)成功")
} catch {
print("??更新單條條記錄(下載狀態(tài)和下載地址):數(shù)據(jù)失敗:\(error)")
}
}
}
刪
/// 刪除對(duì)應(yīng)數(shù)據(jù)
func delete(_ url: String) {
if let db = self.db {
let fileModel = downLoadFile.filter(fielUrl == url)
do {
try db.run(fileModel.delete())
print("?刪除成功:\(url)")
} catch {
print("??刪除失敿坪小:\(url):\(error)")
}
}
}
表字段更新
多表查詢
這些可以從下邊官方文檔看
sqliteswift的git地址渴频。