【问题标题】:Vapor: Saving Byte array to SQLite database?Vapor:将字节数组保存到 SQLite 数据库?
【发布时间】:2018-01-16 08:12:19
【问题描述】:

我无法将大型 (~20k) JSON blob 保存到 SQLite 数据库中。我用builder.bytes("data") 创建了行,它在数据库文件中变成了blobrow。当我尝试保存 Byte aka Bytes 数组时,我收到此错误:

S Q Lite Driver Error: Unsupported Command: Array values not supported.] [Identifier: Fluent.SQLiteDriverError.unsupported] [Possible Causes: using operations not supported by sqlite] [Suggested Fixes: verify data is not corrupt if data type should be supported by sqlite]

错误信息来自实现here的switch语句。

StructuredData 枚举中定义了一个单独的bytes 案例,似乎受 SQLDriver 支持,但我不知道如何到达那里。

这是我的类的定义方式:

最终类 Blob:模型 { 让存储 = Storage()

let uuid: String
let revision: Int
let userId: Identifier
let data: Bytes


init(uuid inUuid: String, revision inRevision: Int, data inData: [UInt8], user: User) throws {
    uuid = inUuid
    userId = try user.assertExists()
    revision = inRevision
    data = inData
}

init(row: Row) throws {
    uuid = try row.get("uuid")
    userId = try row.get(User.foreignIdKey)
    revision = try row.get("revision")
    data = try row.get("data")
}

func makeRow() throws -> Row {
    var row = Row()
    try row.set("uuid", uuid)
    try row.set(User.foreignIdKey, userId)
    try row.set("revision", revision)
    try row.set("data", data)
    return row
}

}

这是使用 Vapor Toolbox 2.0.3 和 Vapor Framework 2.1.3。

【问题讨论】:

  • 能否请您说明您是如何实现makeRow()init(row:) 的?尤其是您如何设置/获取数据属性。
  • 用更多代码更新了我原来的帖子。

标签: swift sqlite vapor


【解决方案1】:

在当前版本的 Vapor 2 中,在将字节数组从/到节点和其他 StructuredDataWrapper 转换时,您不能依赖 Fuzzy get/set 方法。

相反,您必须明确两个方向:

init(row: Row) throws {
    // ...
    data = row["data"]?.bytes ?? []
}

func makeRow() throws -> Row {
    var row = Row()
    // ...
    try row.set("data", StructuredData.bytes(data))
    return row
}

Here 是 GitHub 上的相关问题。

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多