如果不在数组中(FMDB sqlite Swift iOS),如何删除id?
for (var i=0; i < pointId!.count; i++){
do {
try db.executeUpdate("delete from POINTS where ID not in ?) values (?)", values: [pointId![i]])
} catch let error as NSError {
print("failed: \(error.localizedDescription)")
}
}
答案 0 :(得分:1)
FMDB要求使用问号对数组进行完全展开(使用FMDB为long standing issue):
try db.executeUpdate("delete from POINTS where ID not in (?, ?, ?)", values: [id1, id2, id3])
当然,痛点是构建一个带有可变数量问号的SQL查询。 你现在无法避免。
您可以使用像databaseQuestionMarks()这样的实用程序函数(在替代SQLite包装器GRDB.swift中找到):
let questionMarks = databaseQuestionMarks(count: ids.count)
let query = "delete from POINTS where ID not in (\(questionMarks))"
try db.executeUpdate(query, values: ids)
答案 1 :(得分:0)
您也可以这样使用它(不使用问号):
let skippedIdsStr = pointId.joined(separator: ",")
try db.executeUpdate("delete from POINTS where ID not in ("+skippedIds+")", values: [])