中文回答Swift GRDB 4140 dbexecute中给sql语句传的参数带数组如何实现如dbexecutesql delete from chathistory where rowid in ids arguments ids ids提示Cannot convert value of type Int64 to expected dictionary value type Databas
在Swift GRDB 4.14.0中,如果要在db.execute方法中传递带有数组的参数,您可以使用DatabaseValue数组作为参数值。下面是一个示例代码,展示了如何实现这一点:
import GRDB
// 创建一个数组来存储参数值
var arguments: StatementArguments = [:]
// 将数组中的每个值转换为DatabaseValue,并将其添加到arguments字典中
let ids: [Int64] = [1, 2, 3]
arguments["ids"] = ids.map { $0.databaseValue }
// 执行SQL语句
try db.execute(sql: "DELETE FROM chathistory WHERE rowid IN (:ids)", arguments: arguments)
在上述代码中,我们首先创建了一个空的StatementArguments字典来存储参数值。然后,我们将ids数组中的每个值转换为DatabaseValue类型,并将其存储在arguments字典的"ids"键下。最后,我们将arguments字典作为参数传递给db.execute方法来执行带有参数的SQL语句。
希望这可以帮助到您
原文地址: https://www.cveoy.top/t/topic/hEwD 著作权归作者所有。请勿转载和采集!