Module 3
Topic 3

SQLite

A self-contained, high-reliability, embedded SQL database engine — perfect for complex data and relationships.

sqflite package Flutter Docs – SQLite
What Is SQLite?

SQLite is a self-contained, high-reliability, embedded, SQL database engine. It's the most widely deployed database in the world and is perfect for Flutter apps that need:

  • Complex data structures with multiple tables and relationships
  • Efficient queries with filtering, sorting, and joining
  • Large datasets that need to be queried quickly
  • Data integrity with constraints and transactions

✅ When to Use SQLite

  • Complex data with multiple related tables
  • Need for complex queries (JOIN, GROUP BY, ORDER BY)
  • Large datasets that need efficient searching
  • Data integrity with foreign keys and constraints
  • When you need transactions for data consistency
SQLite vs Hive

Hive

  • Key-value NoSQL database
  • Simple API, like a Map
  • No SQL required
  • Great for objects and simple data
  • Real-time updates with listenable
  • Faster for simple operations

SQLite

  • Relational SQL database
  • Complex queries with SQL
  • Multiple tables with relationships
  • Great for complex data and reports
  • Transactions and data integrity
  • Better for large datasets

SharedPreferences

  • Simple key-value storage
  • Primitive types only
  • Small data only
  • Best for app settings

💡 Which One to Choose?

  • Use SQLite – for complex data, relationships, and complex queries
  • Use Hive – for simple object storage with real-time updates
  • Use SharedPreferences – for simple app settings and preferences
Setting Up SQLite

To use SQLite in Flutter, you need to add the sqflite and path packages.

CRUD Operations

Here are the basic CRUD (Create, Read, Update, Delete) operations for SQLite in Flutter.

Complex Queries

SQLite supports complex queries with JOINs, GROUP BY, ORDER BY, and more.

Transactions

Transactions ensure data consistency when performing multiple operations. If any operation fails, all changes are rolled back.

Complete Example: User Management App

Here's a complete Flutter app that uses SQLite for user management with CRUD operations.

Step-by-Step Explanation
1.
Add dependencies – Add sqflite and path to your pubspec.yaml .
2.
Create Database Helper – A singleton class that manages database initialization and migration.
3.
Define the Model – Create a Dart class with toMap() and fromMap() methods for serialization.
4.
Create DAO – Data Access Object with CRUD methods using SQL queries.
5.
Build the UI – Use FutureBuilder or setState to display data from the database.
Best Practices

✅ Do's

  • Use a singleton database helper to avoid opening multiple connections
  • Use transactions for multiple operations
  • Use parameterized queries to prevent SQL injection
  • Use migrations for database schema changes
  • Close the database when the app is terminated
  • Use indexes for frequently queried columns

❌ Don'ts

  • Don't use raw SQL with string concatenation (use parameterized queries)
  • Don't forget to handle exceptions in database operations
  • Don't use SQLite for very simple data (use SharedPreferences)
  • Don't open the database multiple times
Common Mistakes
❌ Mistake 1: Forgetting to close the database

Not closing the database can cause memory leaks and connection issues.

✅ Correct: Close database when done

await db.close(); when the app is terminated or the database is no longer needed.

❌ Mistake 2: Not using transactions for multiple operations

If you insert multiple related records without a transaction, partial failures can corrupt your data.

✅ Correct: Use transactions

Wrap multiple operations in db.transaction() to ensure atomicity.

❌ Mistake 3: Not handling database upgrades

When you change the schema, users with existing databases need a migration path.

✅ Correct: Use onUpgrade

Use the onUpgrade callback in openDatabase() to handle schema migrations.

🎯 Key Takeaway

SQLite is a powerful relational database for Flutter apps that need complex data structures , relationships , and efficient queries . Use transactions for data consistency, parameterized queries for security, and migrations for schema updates. SQLite is the best choice for complex, relational data.