通过SQLite.swift中的正确绑定从任意SQL语句中获取结果

时间:2016-08-15 17:05:30

标签: swift sqlite sqlite.swift

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方法,但我仍然没有发现如何应用它。

0 个答案:

没有答案