我正在尝试从数据库中删除多行。目前,我有一个数组,其中包含我要删除的所有行ID。但是我似乎找不到合适的代码
func deleteMultiple(Id:[Int]) -> NSMutableArray {
sharedInstance.databese!.open()
var i:Int = Id.count
print(Id, i)
let resultSet:FMResultSet! = sharedInstance.databese!.executeQuery("DELETE FROM Info WHERE Id = ?", withArgumentsIn: Id)
let itemInfo:NSMutableArray = NSMutableArray ()
if (resultSet != nil)
{
while resultSet.next() {
let item:Tbl_Info = Tbl_Info()
item.Id = Int(resultSet.int(forColumn: "Id"))
item.Name = String(resultSet.string(forColumn: "Name")!)
item.LastName = String(resultSet.string(forColumn: "LastName")!)
itemInfo.add(item)
}
}
sharedInstance.databese!.close()
return itemInfo
}
答案 0 :(得分:0)
您可以在队列中执行查询,这不会冻结数据库。
如果在事务处理期间发生任何故障,则FMDB提供回滚功能。 查看示例代码。
sharedInstance.databese.dbQueue.inTransaction { db, rollback in
idArray.forEach {id in
do {
let resultSet:FMResultSet! = try db.executeQuery("DELETE FROM Info WHERE Id = ?", withArgumentsIn: Id)
//Do whatever with your resultSet
} catch {
//Error occured during deleting from database.
rollback.pointee = true
}
}
}