【发布时间】:2016-08-16 08:15:52
【问题描述】:
SQLite.swift documentation 表示执行任意 SQL:
let stmt = try db.prepare("SELECT id, email FROM users")
for row in stmt {
for (index, name) in stmt.columnNames.enumerate() {
print ("\(name)=\(row[index]!)")
// id: Optional(1), email: Optional("alice@mac.com")
}
}
我想像这样直接获取值
let stmt = try db.prepare("SELECT id, email FROM users")
for row in stmt {
let myInt: Int64 = row[0] // error: Cannot convert value of type 'Binding?' to specified type 'Int64'
let myString: String = row[1] // error: Cannot convert value of type 'Binding?' to specified type 'String'
}
但是行索引的类型是Binding?,我不知道如何将它转换为我需要的类型。我看到source code 中有一个Statement.bind 方法,但我仍然没有发现如何应用它。
【问题讨论】:
-
您是否尝试过使用
Expression,(例如let myInt: Expression<Int64> = ...) -
@l'L'l,好主意。不幸的是,它给出了同样的错误(无法将
Binding?转换为Expression<Int64>)。我过去曾成功使用过Expression,但现在我试图让任意 SQL 工作(因为this 的问题),我遇到了很多麻烦。
标签: swift sqlite sqlite.swift