Which method is used to create tables when the database is first opened?
The onCreate callback in openDatabase() runs when the database file is created for the first time.
Use SQL queries for structured relational data in Flutter
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
sqflite brings the full power of SQLite to Flutter. SQLite is a relational database, which means your data is organized into tables with rows and columns, and you can write SQL queries to filter, sort, join, and aggregate data. Use sqflite when your app needs complex data relationships (for example, users who have many orders, each with many items), or when you need to search and filter large datasets efficiently. It is more setup than Hive or SharedPreferences, but gives you much more querying power.
1// pubspec.yaml: sqflite: ^2.3.0, path: ^1.8.32import 'package:sqflite/sqflite.dart';3import 'package:path/path.dart';45class DatabaseHelper {6static Database? _database;78Future<Database> get database async {9_database ??= await _initDB();10return _database!;11}1213Future<Database> _initDB() async {14final path = join(await getDatabasesPath(), 'app.db');15return openDatabase(16path,17version: 1,18onCreate: (db, version) async {19await db.execute('''20CREATE TABLE tasks (21id INTEGER PRIMARY KEY AUTOINCREMENT,22title TEXT NOT NULL,23isDone INTEGER DEFAULT 0,24createdAt TEXT25)26''');27},28);29}3031// ── INSERT ──32Future<int> insertTask(Map<String, dynamic> task) async {33final db = await database;34return db.insert('tasks', task);35}3637// ── READ ALL ──38Future<List<Map<String, dynamic>>> getTasks() async {39final db = await database;40return db.query('tasks', orderBy: 'createdAt DESC');41}4243// ── UPDATE ──44Future<int> updateTask(int id, Map<String, dynamic> data) async {45final db = await database;46return db.update('tasks', data, where: 'id = ?', whereArgs: [id]);47}4849// ── DELETE ──50Future<int> deleteTask(int id) async {51final db = await database;52return db.delete('tasks', where: 'id = ?', whereArgs: [id]);53}54}
Use sqflite when you need relational queries like JOINs, WHERE clauses, or ORDER BY. Use Hive for simpler key-value or object storage where read/write speed matters most and you do not need SQL.
SQL power is valuable only when schema evolution is controlled. Plan for change:
Treat migrations as production code. A single broken migration can block app startup for all existing users.
Use SQL queries for structured relational data in Flutter. This lesson adds one more building block for creating stable and user-friendly Flutter apps.
Think in architecture, reliability, and team-level maintainability.
After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.
Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.
Connect this lesson to separation of concerns so your codebase stays maintainable as it grows.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
The onCreate callback in openDatabase() runs when the database file is created for the first time.
Using ? placeholders with whereArgs (parameterized queries) prevents SQL injection attacks.
sqflite returns query results as a List of Maps, where each Map represents a row.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue