sqlite.swift,如何通过名称给出字段值?

时间:2017-05-09 01:42:30

标签: swift sqlite sqlite.swift

作为example

for user in try db.prepare(users.select(id, email)) {
   print("id: (user[id]), email: (user[email])")
   // id: 1, email: alice@mac.com
}
  // SELECT "id", "email" FROM "users"

但我想知道是否可以使用名称但是表达式或索引?

我的意思是:

让id = user [" email"]然后获得该字段的值' email'

似乎不支持?

1 个答案:

答案 0 :(得分:0)

不,SQLite.swift不支持按名称访问行列。

另一种选择是GRDB.swift,如果您愿意,可以按列名访问列值:

// Using raw SQL:
let rows = Row.fetchCursor(db, "SELECT * FROM users")
while let row = rows.next() {
    let id: Int64 = row.value(named: "id")
    let email: String = row.value(named: "email")
}

// Using a "query interface request", and raw row access:
let request = User.all()
let rows = Row.fetchCursor(db, request)
while let row = rows.next() {
    let id: Int64 = row.value(named: "id")
    let email: String = row.value(named: "email")
}

// Using a natural "query interface request":
let users = User.fetchAll(db) // [User]