gpt4 book ai didi

swift - SQLite.swift:由于无法识别的 token 无法检索表计数: ":"

转载 作者:可可西里 更新时间:2023-11-01 01:07:21 26 4
gpt4 key购买 nike

我正在尝试在 Cocoa macOS 应用程序中使用 SQLite.swift 和 Swift 检索表中的记录数。根据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 对象,该对象在 View 加载时从一个单独的 Database 结构函数返回。我相信这是有效且无关的,因为其他代码区域正在使用相同的方法成功打开与数据库的连接。

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

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

enter image description here

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

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 步骤来删除、更新和安装项目 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

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

关于swift - SQLite.swift:由于无法识别的 token 无法检索表计数: ":",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55755296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com