Swift SQLite I/O 错误

Ric*_*son 3 sqlite io ios swift swift3

我不断收到以下错误:

致命错误:“尝试!” 表达式意外引发错误:磁盘 I/O 错误(代码:10):文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType .swift,第 178 行

当我实现以下代码时:

try db!.transaction() {
    hold = true
    for obj in object {
        if table.rawValue == DBTableNames.Products.rawValue {
            let product : ProductObject =  obj as! ProductObject
            insert = self.productsTable.insert(
                self.productcode <- product.productcode,
                self.admincost <- product.admincost,
                self.allocatedstock <- product.allocatedstock,
                self.availablestock <- product.availablestock,
                self.backorderstock <- product.backorderstock,
                self.barcode <- product.barcode,
                self.binno <- product.binno,
                self.casesperlayer <- product.casesperlayer,
                self.clearanceprice <- product.clearanceprice,
                self.exportprice <- product.exportprice,
                self.fulldescription <- product.fulldescription,
                self.gqcoded <- product.gqcoded,
                self.group <- product.group,
                self.innerbarcode <- product.innerbarcode,
                self.innerqty <- product.innerqty,
                self.lastPoDate <- product.lastPoDate as Date,
                self.layersperpallet <- product.layersperpallet,
                self.longdescription1 <- product.longdescription1,
                self.mtdsales <- product.mtdsales,
                self.onorderstock <- product.onorderstock,
                self.outerbarcode <- product.outerbarcode,
                self.packqty <- product.packqty,
                self.palletqty <- product.palletqty,
                self.physicalstock <- product.physicalstock,
                self.rollingsales <- product.rollingsales,
                self.rsp <- product.rsp,
                self.shortdescription <- product.shortdescription,
                self.threemonth <- product.threemonth,
                self.warehouse <- product.warehouse,
                self.weight <- product.weight,
                self.wholesaleprice <- product.wholesaleprice,
                self.ytdsales <- product.wholesaleprice)
        }
    }
    try self.db!.run(insert)
}
Run Code Online (Sandbox Code Playgroud)

我理解它是一个 I/O 错误,但我不明白还有什么可能访问数据库阻止插入

我正在使用以下固件: https: //github.com/stephencelis/SQLite.swift

Dev*_*B2F 5

如果您在模拟器上遇到错误,请阅读第 2 部分。如果错误发生在实际设备上,请尝试第 1 部分,但也请尝试在模拟器中运行您的应用程序并尝试我的答案的第 2 部分。

  1. 如果您使用实际设备并收到 SQLite I/O 错误:错误的原因可能是数据库文件的位置。地点是:

    /var/mobile/Containers/Data/Application/someid/Documents/somedb.sqlite3

我通过将 db 文件移动到以下位置消除了错误:

/var/mobile/Containers/Data/Application/someid/Library/somedb.sqlite3
Run Code Online (Sandbox Code Playgroud)

所以而不是:

static var destinationSqliteURL: URL!
let myPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
destinationSqliteURL = myPath.appendingPathComponent("somedb.sqlite3")
Run Code Online (Sandbox Code Playgroud)

我用了:

static var destinationSqliteURL: URL!
let myPath = fileManager.urls(for: .libraryDirectory, in: .allDomainsMask)[0]
destinationSqliteURL = myPath.appendingPathComponent("somedb.sqlite3")
Run Code Online (Sandbox Code Playgroud)
  1. 如果您使用模拟器并收到 SQLite I/O 错误:

您应该验证文件是否正常(使用 xcode 模拟器),通过打印 myPath 并 cd 到包含数据库的文件夹中,然后在终端中使用以下命令打开数据库文件:

sqlite3 somedb.sqlite3

可能那里没有文件或者它已损坏并且必须更换。