---
title: "How do you use SQLite in Flutter?"
description: "Use the sqflite package to open a SQLite database, create tables, and insert and query rows in a Flutter app, with notes on desktop and drift."
url: "https://bugsee.com/answers/flutter/flutter-sqlite/"
---

# How do you use SQLite in Flutter?

Add the `sqflite` and `path` packages, open the database with `openDatabase`, create tables in `onCreate`, then use `insert` and `query` for data.

```dart
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

Future<Database> openAppDb() async {
  final path = join(await getDatabasesPath(), 'app.db');
  return openDatabase(
    path,
    version: 1,
    onCreate: (db, version) => db.execute(
      'CREATE TABLE notes(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL)',
    ),
  );
}

Future<void> demo() async {
  final db = await openAppDb();
  await db.insert('notes', {'title': 'Buy milk'});
  final rows = await db.query('notes', where: 'title LIKE ?', whereArgs: ['%milk%']);
  print(rows);   // [{id: 1, title: Buy milk}]
}
```

- Always pass user input through `whereArgs`, never by string concatenation.
- Raise `version` and handle `onUpgrade` when you change the schema.
- `sqflite` targets Android, iOS and macOS. For desktop and tests use `sqflite_common_ffi`.
- For type-safe queries and migrations, `drift` builds on SQLite with code generation.

## Related questions

- [What is build_runner in Flutter and how do you use it?](https://bugsee.com/answers/flutter/flutter-build-runner/)
- [How do you run a background service in Flutter?](https://bugsee.com/answers/flutter/flutter-background-service/)
