我正在尝试写入我创建的语录数据库。如果用户选择“收藏”按钮,它将在SQLITE数据库的“favorite”列中添加1(或true)值。但由于某些原因,我无法向数据库写入数据,执行以下代码时会出现问题:
@IBAction func addFavorite(_ sender: Any) {
var configuration = Configuration()
configuration.readonly = false
let dbPath = Bundle.main.path(forResource: "data", ofType: "db")!
let dbQueue = try! DatabaseQueue(path: dbPath, configuration: configuration)
try! dbQueue.inDatabase { db in
try db.execute("""
UPDATE quotes SET favorite = 1 WHERE quote = ?;
""",
arguments: [quoteLabel.text])
}
我希望结果能够写入数据库并将 1 放置在收藏列中,但可悲的是事实并非如此。我收到了这个错误:
Thread 1: 致命错误:'try!'表达式意外地引发了错误:SQLite 错误 8,语句为
UPDATE quotes SET favorite = 1 WHERE quote = ?
参数为["Abstinence is the great strengthener and clearer of reason."] :尝试写入只读数据库我尝试过使用
chmod 755
更改数据库权限,但也无济于事。我是否遗漏了什么?