基于Swift使用FMDB实现(增删改查+搜索+聊天置顶)

基于Swift使用FMDB实现(增删改查+搜索+聊天置顶),第1张

概述Pod引入FMDB框架(记得加use_frameworks!),引入头文件import FMDB 写一个DBManager类 class DBManager: NSObject { static let shareManager = DBManager() var dbQueue: FMDatabaseQueue? func openDB() { let 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 {    //给String扩展一个stringByAppendingPathComponent方法     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";        //根据istop字段降序输出        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        // liKE '%Java%'查询des字段中包含Java的,liKE 'Java*' 查询以Java开头的,'*Java查询以Java结尾的'        // 查询 table_DETAIL 表中包含string的model        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}
DEMO效果 DEMO地址 总结

以上是内存溢出为你收集整理的基于Swift使用FMDB实现(增删改查+搜索+聊天置顶)全部内容,希望文章能够帮你解决基于Swift使用FMDB实现(增删改查+搜索+聊天置顶)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1064332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存