iOS FMDB事务处理示例代码
2024-07-18 本文已影响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()
}