gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:28:39 26 4
gpt4 key购买 nike

作为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"

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

我的意思是:

let id = user["email"] 然后获取'email'字段的值

好像不支持?

最佳答案

不,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]

关于swift - sqlite.swift,如何通过名称给出字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43859696/

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