Home > Enterprise >  Flutter SQFlite Delete Multiple by List of integer IDs
Flutter SQFlite Delete Multiple by List of integer IDs

Time:11-17

SQFlite in Flutter

Following code deletes one row by the given ID

Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(myTable, where: 'id = ?', whereArgs: [id]);
}

but How to delete multiple IDs simultaneously?

I have variable list: List<int> toBeDeleted = [5, 4, 8, ...]

I tried giving a list in whereArgs but it's not working. What I'm looking for is being able to "Delete Selected Multiple items at once".

CodePudding user response:

You can use the SQLite IN clause:

await db.delete(myTable, where: 'id IN (?,?,?)', whereArgs: [id1, id2, id3]);

Since the list size can change, having the proper number or ? can be solved using the following solution: List.filled(inArgsCount, '?').join(',')

var list = ['5', '4', '8'];
return await db.delete('myTable',
  where: 'id IN (${List.filled(list.length, '?').join(',')})',
  whereArgs: list);

More information here https://github.com/tekartik/sqflite/blob/master/sqflite/doc/sql.md#parameters

  • Related