【问题标题】:In the sqlite.swift framework is there a way to prepare the statement once ahead of time and then bind the variable just before execution?在 sqlite.swift 框架中,有没有办法提前准备好语句,然后在执行前绑定变量?
【发布时间】:2017-05-04 02:46:02
【问题描述】:

我将 SQLite 用于我的持久存储。 我使用基于主键的字典来存储内存:var localContext = [String: GrandLite]()

我使用下面的函数从字典中检索对象,或者从数据库中检索对象,然后存储在字典中。这个函数被频繁调用,我正在尝试优化它。

class func retrieveByKey<T: GrandLite>(aType: [T], thisKey: String) -> T? {
    let thisStack = FoodysLiteStack.thisDataStack
    if let thisObject = thisStack.localContext[thisKey] as? T {
        return thisObject
    } else {
        do {
            let db = thisStack.localDatabase
            let thisTable = T.getTable()
            if let thisRow = try db.pluck(thisTable.filter(uuidKeyLite == thisKey)) {
                let thisObject = T(withRow: thisRow)
                thisStack.localContext[thisKey] = thisObject
                return thisObject
            } else {
                return nil
            }
        } catch {
            NSLog("WARNING: Unhandled error for T retrieveByKey")
            return nil
        }
    }
}

据我了解 sqlite.swift pluck 基本上是一个限制为 1 的准备。此外,准备编译 SQL 语句,绑定变量并执行它。每次调用此函数时,我都试图避免 SQLite 编译时间。

在 sqlite.swift 框架中,有没有一种方法可以提前准备好语句,然后在执行前绑定变量?您可以使用 db.prepare 和 db.run 对插入和更新执行此操作,但我看不到为已经准备好的 select 语句绑定变量的方法。我可能想多了,thisTable.filter(uuidKeyLite == thisKey) 上的 SQLite 编译时间可能很小。

【问题讨论】:

    标签: ios sqlite sqlite.swift


    【解决方案1】:

    该框架尽最大努力将这些细节抽象出来。

    保留准备好的语句的唯一方法是使用 execute raw SQL 的函数。

    是的,在 SQLite 中,准备开销通常很小;最典型的性能问题来自于没有将多个语句组合成一个事务。

    【讨论】:

      【解决方案2】:

      我可能想多了,thisTable.filter(uuidKeyLite == thisKey) 上的 SQLite 编译时间可能很小。

      我想你会的。尤其是使用 SQLite.swift,语句编译时间与其内部机制相比目前可以忽略不计。见Comparing the Performances of Swift SQLite libraries

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-20
        • 2013-04-16
        • 2020-12-03
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多