I’m trying to batch-delete all of the records in a list of ids using SQLiteOpenHelper
without luck. This is something like what I had in my code:
class DBHelper(context: Context?, factory: SQLiteDatabase.CursorFactory?) :
SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_VERSION) {
...
private fun <T>buildIdsArguments(ids: Array<T>): Pair<Array<String>, String> {
val args = mutableListOf<String>()
val placeholders = mutableListOf<String>()
for (id in ids) {
args.add("'$id'")
placeholders.add("?")
}
return Pair(args.toTypedArray(), placeholders.joinToString())
}
fun <T>deleteMany(db: SQLiteDatabase, table: DBTable, ids: Array<T>) {
val (args, placeholders) = buildIdsArguments(ids)
val deleted = db.delete(table.name, "id in ($placeholders)", args)
if (deleted != ids.size) {
throw Exception("An error occurred while deleting the records")
}
}
}
I’m trying to call the deleteMany
method of the DBHelper
class and I’m always getting the "An error occurred while deleting the records"
error I set in case the number of deleted records don’t match the number of ids.
Do you know what I’m doing wrong?