SQLite.swift:无法检索 table 计数,因为无法识别令牌:“:”

SQLite.swift: Can't retrieve table count due to unrecognized token: ":"

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

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

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

Fatal error: 'try!' expression unexpectedly raised an error: unrecognized token: ":" (code: 1): file...

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

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() {}
}

任何帮助都将不胜感激。提前致谢!

正如我所怀疑的那样,它原来是 SQLite.swift 本身的问题。我在几天前报告此问题的 GitHub 上发现了以下 SQLite.swift 个问题。

问题如下,据记者描述,tanzolone

In Xcode 10.2 beta 4 the macro#functionshows a different behaviour. As SQLite.swift relies on #function in different subroutines for the composition of SQLite queries, several bugs linked to invalid SQLite statements seem to be introduced.

I have just run the SQLite.swift tests with Xcode 10.2 beta 4 and there are several failures linked to this issue.

An example as follows: In the following helper function (Helpers.swift line 107):

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

when called from

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

the value of the function when running in Xcode 10.2 beta 4 is count(_:) instead of count. This leads to invalid SQLite statements of the type SELECT count(_:)(*) myTable.

项目维护者现在最晚 master HEAD 解决了这个问题(目前 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 步骤来删除、更新和安装项目 pods:

$ 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

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