美文网首页
iOS FMDB事务处理示例代码

iOS FMDB事务处理示例代码

作者: 可乐小子 | 来源:发表于2024-07-18 14:45 被阅读0次

    import FMDB

    let dbPath = "/path/to/database.sqlite"
    let database = FMDatabase(path: dbPath)

    if database.open() {
    let createTableSQL = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"
    let result = database.executeUpdate(createTableSQL, withArgumentsIn: [])

    if !result {
        print("Failed to create table")
    }
    
    let insertSQL = "INSERT INTO users (id, name) VALUES (?, ?)"
    let data = [
        ["1", "John"],
        ["2", "Amy"]
    ]
    
    database.beginTransaction()
    
    for record in data {
        let result = database.executeUpdate(insertSQL, withArgumentsIn: record)
        
        if !result {
            print("Failed to insert record")
        }
    }
    
    database.commit()
    database.close()
    

    }

    相关文章

      网友评论

          本文标题:iOS FMDB事务处理示例代码

          本文链接:https://www.haomeiwen.com/subject/scirhjtx.html