Insert multiple records in Sqflite
If you have a list of objects you want to insert, you can do it like this (it’s not a very clean code, but it works):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | insertClients(List<Client> clients) async { final db = await database; var buffer = new StringBuffer(); clients.forEach((c) { if (buffer.isNotEmpty) { buffer.write(",\n"); } buffer.write("('"); buffer.write(c.firstName); buffer.write("', '"); buffer.write(c.lastName); buffer.write("', '"); buffer.write(c.address); buffer.write("')"); }); var raw = await db.rawInsert("INSERT Into Clients (firstName,lastName,address)" " VALUES ${buffer.toString()}"); return raw; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.