Swift GRDB SQL 参数数组插值不起作用

问题描述 投票:0回答:1

我正在使用最新版本的 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 子句中,看起来插值不起作用。

关于如何解决这个问题有任何提示吗?

swift sqlite grdb
1个回答
0
投票

看起来第一个参数缺少撇号,我建议使用以下代码将数组转换为字符串

let params = excluded.map { "'\($0)'" }.joined(separator: ",")
© www.soinside.com 2019 - 2024. All rights reserved.