其他分享
首页 > 其他分享> > swift5 FMDB的基本使用

swift5 FMDB的基本使用

作者:互联网

使用OC FMDB,然后桥接 创建一个桥接文件,

ifndef Bridge_Header_h

define Bridge_Header_h

import "FMDB.h"

endif /* Bridge_Header_h */

不能使用 cocoapods管理,只能把框架源码导入swift项目里面。通过桥接文件引入头文件。

//
//  SQLiteTool.swift
//  FMDBDemo
//
//  Created by lujun on 2022/1/30.
//

import UIKit

class SQLiteTool {
    static let sharedInstance  = SQLiteTool()
    var path: String  {
        let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let paths =  (docPath! as NSString).appendingPathComponent("LJ.db")
        return paths
    }
    class  func getPath() -> String {
        let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let paths =  (docPath! as NSString).appendingPathComponent("LJ.db")
        return paths
    }
    var db: FMDatabase?
    var dbQueu: FMDatabaseQueue = FMDatabaseQueue(path: SQLiteTool.getPath())!
    private init() {
        debugPrint(path)
        db = FMDatabase(path: path)
        if db!.open() {
            debugPrint("数据库打开")
            self.createTable()
        }else{
            debugPrint("数据库失败")
        }
    }
    
    func createTable() {
        let sql = "create table if not exists t_stu(id integer primary key autoincrement, name text not null,age integer, score real default 60)"
        let result = db?.executeUpdate(sql, withArgumentsIn: [])
        if result! {
            debugPrint("yes")
        }else{
            debugPrint("No")
        }
        
    }
    func createTable1(){
        let sql = "create table if not exists t_stu(id integer primary key autoincrement, name text not null,age integer, score real default 60)"
        dbQueu.inDatabase { (db: FMDatabase!) in
            let result = db?.executeUpdate(sql, withArgumentsIn: [])
            if result! {
                debugPrint("yes")
            }else{
                debugPrint("No")
            }
            
            
        }
    }
    func executeStatments(){
        let sql = "insert into t_stu(name,age,score) values ('lujun',15,80);insert into t_stu(name,age,score) values ('dajun',15,88)"
        let result = db?.executeStatements(sql)
        if result! {
            debugPrint("yes")
        }else{
            debugPrint("失败")
        }
    }
    func executeStatments1(){
        let sql = "insert into t_stu(name,age,score) values ('lujun',15,80);insert into t_stu(name,age,score) values ('dajun',15,88)"
        dbQueu.inDatabase { (db: FMDatabase!) in
            let result = db?.executeStatements(sql)
            if result! {
                debugPrint("yes")
            }else{
                debugPrint("失败")
            }
        }
        
    }
    func inserRecord(){
        // for _ in 0...100000 {
        let  sql = "insert into t_stu(name,age,score) values ('lujun',15,80)"
        let result = db?.executeUpdate(sql, withArgumentsIn: [])
        if result! {
            // debugPrint("yes")
        }else{
            debugPrint("No")
        }
        // }
        
    }
    
    func inserRecord1(){
        let  sql = "insert into t_stu(name,age,score) values ('lujun',15,80)"
        dbQueu.inDatabase { (db: FMDatabase!) in
            let result = db?.executeUpdate(sql, withArgumentsIn: [])
            if result! {
                // debugPrint("yes")
            }else{
                debugPrint("No")
            }
        }
    }
    
    func queryAll(){
        let sql = "select * from t_stu"
        guard let resultSet =   db?.executeQuery(sql, withArgumentsIn: [])
        else{
            
            return
        }
        while resultSet.next() {
            let name = resultSet.string(forColumn: "name")
            let age = resultSet.int(forColumn: "age")
            let score = resultSet.double(forColumn: "score")
            
            debugPrint(name!,age,score)
        }
    }
    
    //事务
    func transaction(){
        let sql = "insert into t_stu(name,age,score) values ('lujun',15,80)"
        let sql2 = "insert into t_stu(name,age,score) values ('lujun',52,82)"
    
        dbQueu.inTransaction { (db : FMDatabase! , rollback )in
            let result1 = db?.executeUpdate(sql, withArgumentsIn: [])
            let result2 = db?.executeUpdate(sql2, withArgumentsIn: [])
            if result1! && result2! {
                debugPrint("成功")
            }else{
              let _ =  rollback.move()
                
                
            }
        }
        
    }
    
}

标签:基本,name,db,FMDB,debugPrint,score,let,sql,swift5
来源: https://www.cnblogs.com/Johnson-Swift/p/15857271.html