提问人:V_49 提问时间:11/19/2020 最后编辑:V_49 更新时间:11/24/2020 访问量:244
如何在 Xcode 的 iOS 应用程序中包含读写(现有)SQLite 数据库?
How to include a read-write (existing) SQLite database in an iOS app from Xcode?
问:
我正在尝试在我的 iOS 应用程序中包含来自 Xcode 的预先创建的 sqlite 数据库。当应用从 Web 服务器收到某些通知(JSON 格式)时,需要更新数据库。
该数据库包含在 Xcode 文件中,我在检查器中看到了它。
我用于在数据库中插入数据的函数如下:
Import SQLite
func insertItem(item: Item){
let id = Expression<Int64>("id")
let description = Expression<String>("description")
let bundle = Bundle.main
let path = bundle.path(forResource: "DatabaseLocale", ofType: "sqlite")!
let db = try! Connection("\(path)")
let items = Table("Items")
let insert = items.insert(id <- Int64(item.id), description <- item.description)
do{
let rowid = try! db.run(insert)
} catch {
print("cannot modify the database")
}
do{
for itemID in try db.prepare(items){
print(itemID[id])
print(itemID[description])
}
} catch {
print("couldn't connect to database")
}
}
现在,当我在模拟器中运行应用程序时,我得到了我想要的结果,因此应用程序接收的新数据被正确地写入数据库,但每当我使用我的物理 iPhone 时,我都会收到一个错误:
Fatal error: 'try!' expression unexpectedly raised an error: attempt to write a readonly database (code: 8)
如何使 iPhone 应用程序内的 SQLite 数据库可修改?
答: 暂无答案
评论