前端之家收集整理的这篇文章主要介绍了
基于Swift使用FMDB实现(增删改查+搜索+聊天置顶),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Pod
引入FMDB
框架(记得加use_frameworks!
),引入头文件import FMDB
写一个DBManager类
class DBManager: NSObject {
static let shareManager = DBManager()
var dbQueue: FMDatabaseQueue?
func openDB() {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true).first
let userPath = documentDirectory?.stringByAppendingPathComponent(path: "data10001")
if !FileManager.default.fileExists(atPath: userPath!) {
guard ((try? FileManager.default.createDirectory(atPath: userPath!,withIntermediateDirectories: false,attributes: nil)) != nil) else {
return
}
}
let fileName = userPath?.stringByAppendingPathComponent(path: "person.db")
dbQueue = FMDatabaseQueue(path: fileName)
}
}
extension String {
func stringByAppendingPathComponent(path: String) -> String {
let nsString = self as NSString
return nsString.appendingPathComponent(path)
}
}
每一张表写一个DAO(Data Access Object),写sql语句操作数据库,暴露接口,例如TABLE_LIST
表的ListDAO
的部分代码
static let shareDAO = ListDAO()
func creatTable() {
let sqlString = "CREATE TABLE IF NOT EXISTS TABLE_LIST('personId' Integer,'name' Text,'isTop' Integer)"
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
guard (db?.executeUpdate(sqlString,withArgumentsIn: [])) != nil else {
return
}
})
}
func dropTable() {
let sqlString = "DROP TABLE TABLE_LIST"
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
guard (db?.executeUpdate(sqlString,withArgumentsIn: [])) != nil else {
return
}
})
}
func insertData(model: ListModel) {
let sqlString = "INSERT OR REPLACE INTO TABLE_LIST(personId,name,isTop) values (?,?,?)"
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
guard (db?.executeUpdate(sqlString,withArgumentsIn: [model.personId,model.name,model.isTop]))! else {
return
}
})
}
func getDataList() -> Array<ListModel> {
var resultArray = Array<Any>()
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
let sqlString = "SELECT * FROM TABLE_LIST ORDER BY isTop DESC";
guard let set = try? db?.executeQuery(sqlString,values: []) else {
return
}
while (set?.next())! {
let model = ListModel()
model.personId = Int((set?.int(forColumn: "personId"))!)
model.name = set?.string(forColumn: "name")
model.isTop = (set?.bool(forColumn: "isTop"))!
resultArray.append(model)
}
set?.close()
})
return resultArray as! Array<ListModel>
}
func deleteDataOfDataList(personId: Int) -> Bool {
var result: Bool = false
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
let sqlString = "DELETE FROM TABLE_LIST WHERE personId = ?"
result = (db?.executeUpdate(sqlString,withArgumentsIn: [personId]))!
})
return result
}
func setTopWithPersonId(personId: Int,isTop: Bool) -> Bool {
var result: Bool = false
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
let sqlString = "UPDATE TABLE_LIST SET isTop = ? WHERE personId = ?"
result = (db?.executeUpdate(sqlString,withArgumentsIn: [isTop,personId]))!
})
return result
}
TABLE_DETAIL
表的DetailDAO
的部分代码
func getDataListWithString(string: String) -> Array<Any> {
var resultArray = Array<Any>()
DBManager.shareManager.dbQueue?.inDatabase({ (db) in
let sqlString = "SELECT * FROM TABLE_DETAIL WHERE des LIKE '%" + string + "%'"
guard let set = try? db?.executeQuery(sqlString,values: []) else {
return
}
while (set?.next())! {
let model = DetailModel()
model.desId = Int((set?.int(forColumn: "desId"))!)
model.des = set?.string(forColumn: "des")
resultArray.append(model)
}
set?.close()
})
return resultArray
}