我正在使用最新版本的 GRDB,并且我尝试将如下参数数组传递给 SQLRequest:
class GamesDao {
private var dbQueue: DatabaseQueue?
init(dbQueue: DatabaseQueue) {
self.dbQueue = dbQueue
}
func getAllExcluding(excluded: [String]) throws -> [GameAndStuff] {
let params = excluded.joined(separator: "', '")
return try dbQueue!.read { db in
return try SQLRequest(
"""
SELECT
g._id as g__id
FROM games as g
WHERE g._id NOT IN (\(params))
"""
).fetchAll(db)
}
}
}
但是,GRDB 库正在处理错误的字符串插值,如下所示:
SELECT
g._id as g__id
FROM games as g
WHERE g._id NOT IN (?)`, arguments: ["game1\', \'game2\', \'game3\', \'game4\', \'game5'"]
并且没有将实际参数注入到 IN 子句中,看起来插值不起作用。
关于如何解决这个问题有任何提示吗?
看起来第一个参数缺少撇号,我建议使用以下代码将数组转换为字符串
let params = excluded.map { "'\($0)'" }.joined(separator: ",")