【问题标题】:SQLite.swift: Can't retrieve table count due to unrecognized token: ":"SQLite.swift:由于无法识别的令牌而无法检索表计数:“:”
【发布时间】:2019-04-19 00:49:20
【问题描述】:

我正在尝试在 Cocoa macOS 应用程序中使用 SQLite.swift 和 Swift 检索表中的记录计数。根据README,这可以通过以下方式实现:

let users = Table("users")
...
let count = try db.scalar(users.count)

但是,当执行此代码时(从按钮单击事件处理程序),将引发以下异常:

致命错误:“试试!”表达式意外引发错误:无法识别的令牌:“:”(代码:1):文件...

违规代码是下面的db 访问行。当视图加载时,db 对象分配有从单独的 Database 结构函数返回的 Connection 对象。我相信这是有效且不相关的,因为其他代码区域正在使用相同的方法成功打开与数据库的连接。

let login = Login()
var db : Connection?
...
let count = try! db?.scalar(login.table.count)

这是它产生的查询表达式。在文本中,这似乎是SELECT count(_:)(*) FROM login

然而,有趣的是,如果我使用以下行,则会返回正确的结果。

let count = try! db?.scalar(“SELECT COUNT(*) FROM login”)

这个Login 对象的模型是:

import SQLite

public struct Login {
    let table = Table("login")

    // Bunch of properties for various record fields
    init() {}
}

我们将不胜感激。提前致谢!

【问题讨论】:

    标签: swift xcode macos sqlite sqlite.swift


    【解决方案1】:

    事实证明这是SQLite.swift 本身的问题,正如我所怀疑的那样。几天前,我在 GitHub 上发现了以下 SQLite.swift 问题,报告了此问题。

    如记者tanzolone所述,问题如下:

    在 Xcode 10.2 beta 4 中,macro#function 表现出不同的行为。作为 SQLite.swift 依赖于不同子例程中的#function SQLite 查询的组成,几个与无效 SQLite 相关的错误 似乎引入了语句。

    我刚刚使用 Xcode 10.2 beta 4 和 有几个与此问题相关的故障。

    一个例子如下: 在以下辅助函数中(Helpers.swift 第 107 行):

    func wrap<T>(_ expression: Expressible, function: String = #function) -> Expression<T> {
        return function.wrap(expression)
    }
    

    调用时

    static func count(_ star: Star) -> Expression<UnderlyingType>
    

    在 Xcode 10.2 beta 4 中运行时该函数的值为 count(_:) 而不是 count。这会导致无效的 SQLite SELECT count(_:)(*) myTable 类型的语句。

    项目维护者现在已经解决了这个问题,最晚 HEADmaster(目前是 1a908a7da11852f252e7c6b6366a4d9f8a7d5272)。

    在我的项目中,我的Podfile 中需要更新的行如下:

    pod 'SQLite.swift/SQLCipher', :git => 'https://github.com/stephencelis/SQLite.swift.git', :commit => 'ed8f603f856e9f1f4adb76b63b7234547257ec5a'
    

    已更新为:

    pod 'SQLite.swift/SQLCipher', :git => 'https://github.com/stephencelis/SQLite.swift.git', :branch => 'master'
    

    或者,可以引用提交本身:

    pod 'SQLite.swift/SQLCipher', :git => 'https://github.com/stephencelis/SQLite.swift.git', :commit => '1a908a7da11852f252e7c6b6366a4d9f8a7d5272'
    

    为了更新此参考,我在项目根目录中执行了以下 CLI 步骤来删除、更新和安装项目 pod:

    $ pod cache clean --all
    $ rm -rf Pods/
    $ rm Podfile.lock
    $ open -a Xcode Podfile   # Updated the SQLite.swift project ref to the above
    $ pod install
    

    这样就解决了问题,不再抛出异常。

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多