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()
}
网友评论