在swift中使用FMDB框架进行数据库操作(适用于swift2.0)

import UIKit
class SqliteTools: NSObject {
    
    let dbPath:String
    let dbBase:FMDatabase
    
//使用单例创建数据库
    class func sharedSocketTools()->SqliteTools{
        
        struct YRSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:SqliteTools? = nil
        }
        dispatch_once(&YRSingleton.predicate,{
            YRSingleton.instance=SqliteTools()
            }
        )
        return YRSingleton.instance!
    }
// MARK: >> 创建数据库,打开数据库
    override init() {
        let documentsFolder:String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
        let path = documentsFolder.stringByAppendingString("test.sqlite")
        self.dbPath = path
        //创建数据库
        dbBase =  FMDatabase(path: self.dbPath as String)
        
        print("path: ---- \(self.dbPath)")
        
        //打开数据库
        if dbBase.open(){
            
            let userSql:String = "CREATE TABLE IF NOT EXISTS t_user(id INTEGER PRIMARY KEY AUTOINCREMENT,uid TEXT, password TEXT, isRemember integer, isAutoLogin integer)"
            if dbBase.executeUpdate(userSql, withArgumentsInArray: nil){
                print("数据库创建成功!")
            }else{
                print("数据库创建失败!failed:\(dbBase.lastErrorMessage())")
            }
        }else{
            print("Unable to open database!")
            
        }
    }
    // MARK: >> 增加新用户
    func addPerson(user:UserInfor) {
        
        dbBase.open();
        
        let arr:[AnyObject] = [user.uid!,
            user.password!,
            user.isRemember!,
            user.isAutoLogin!];
        if !self.dbBase.executeUpdate("insert into t_user (uid ,password, isRemember,isAutoLogin) values (?, ?, ?, ?)",
            withArgumentsInArray: arr) {
            print("添加1条数据失败!: \(dbBase.lastErrorMessage())")
        }else{
            print("添加1条数据成功!: \(user.uid)")
            
        }
        
        dbBase.close();
    }
    // MARK: >> 删除一个用户
    func deletePerson(user:UserInfor) {
        
        dbBase.open();
        if !self.dbBase.executeUpdate("delete from t_user where uid = (?)", withArgumentsInArray: [user.uid!]) {
            print("删除1条数据失败!: \(dbBase.lastErrorMessage())")
        }else{
            print("删除1条数据成功!: \(user.uid)")
        }
        dbBase.close();
    }
    // MARK: >> 修改用户信息
    func updatePerson(user:UserInfor) {
        dbBase.open();
        let arr:[AnyObject] = [user.password!,
            user.isRemember!,
            user.isAutoLogin!,
            user.uid!];
        if !self.dbBase .executeUpdate("update t_user set password = (?), isRemember = (?),isAutoLogin = (?) where uid = (?)", withArgumentsInArray:arr) {
            print("修改1条数据失败!: \(dbBase.lastErrorMessage())")
        }else{
            print("修改1条数据成功!: \(user.uid)")
        }
        dbBase.close();
    }
    // MARK: >> 查询所有用户信息
    func selectPersons() -> Array<UserInfor> {
        dbBase.open();
        var persons=[UserInfor]()
        
        if let rs =
            dbBase.executeQuery("select uid,password,isRemember,isAutoLogin from t_user",withArgumentsInArray: nil) {
            while rs.next() {
                let uid:String = String(rs.stringForColumn("uid"))
                let password:String = String(rs.stringForColumn("password"))
                let isRemember:Int = Int(rs.intForColumn("isRemember"))
                let isAutoLogin:Int = Int(rs.intForColumn("isAutoLogin"))
                let user:UserInfor = UserInfor(uid: uid,
                    password: password,
                    isRemember: isRemember,
                    isAutoLogin: isAutoLogin)
                persons.append(user)
            }
        } else {
            
            print("查询失败 failed: \(dbBase.lastErrorMessage())")
            
        }
        dbBase.close();
        
        return persons
        
    }
    //查询以 ** 开头的id
    func selectUserId(id:String) -> Array<String>{
        dbBase.open();
        var uidAry=[String]()
        if let rs =
            dbBase.executeQuery("SELECT * FROM t_user WHERE uid like '\(id)%%'",withArgumentsInArray: nil){
                
                while rs.next() {
                    
                    let uid:String = String(rs.stringForColumn("uid"))
                    uidAry.append(uid)
                }
        }
        dbBase.close();
        return uidAry
    }
    // MARK: >> 删除所有用户
    func deleteAllUser(){
        dbBase.open();
        if !self.dbBase.executeUpdate("delete from t_user",
            withArgumentsInArray: nil){
            print("删除1条数据失败!: \(dbBase.lastErrorMessage())")
        }
        dbBase.close();
    }


参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

撸代码不听歌不舒服斯基

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值