Back to results for “SQLite”
Flutter › SQLite & Local Databases (sqflite)

How do you properly create a table and perform basic insert, query, update, and delete operations using sqflite's own available methods?

Beginner

Answer

sqflite provides genuinely convenient dedicated methods for each of the basic standard database operations, including insert for adding a genuinely new row, query for retrieving genuinely existing rows optionally filtered by a specific given condition, update for properly modifying an already existing row, and delete for properly removing a given row, and each one of these methods accepts a Map representing that particular row's own data, letting you avoid needing to manually write raw SQL strings for these particular basic, genuinely everyday common operations.
await db.insert('orders', {'total': 49.99});
final orders = await db.query('orders', where: 'total > ?', whereArgs: [20]);
Real-world example An expense tracking app inserts a genuinely new expense record using db.insert and later queries for every single expense over a specific given threshold amount using db.query, without needing to manually write any raw SQL query strings for these particular common everyday operations.

Common follow-ups: How do you actually properly and correctly use where and whereArgs together to safely avoid SQL injection?;What genuinely happens if you try to insert a row that violates an already existing table constraint?