SQLite database is a standard SQL based database engine. It is small and time-tested database engine. However, you will often find that you need to save data locally in your app so one of the technique to save data locally is using SQLite database.
You may also check our flutter app development services
Flutter apps can make use of the SQLite databases with the help sqflite
plugin available on pub.dev.
SQLite plugin for Flutter has some below features:
- Supports transactions and batches.
- Automatic version management during open.
- Helpers for insert/query/update/delete queries.
- DB operation executed in a background thread on iOS and Android.
Let’s get started with how we can install the plugin.
1 2 3 |
dependencies: ... sqflite: ^1.3.0 |
Add the dependency in your pubspec.yaml file.
After that, import the class in the file you want to use the package.
1 |
import 'package:sqflite/sqflite.dart'; |
Then, to initialize and open the database, do below part.
1 |
var db = await openDatabase('my_db.db'); |
Furthermore, Below are few helper methods provided by the dependency.
Insert
1 2 3 4 |
Future<Model> insert(Model model) async { model.id = await db.insert(table, model.toMap()); return model; } |
Delete
1 2 3 |
Future<int> delete(int id) async { return await db.delete(table, where: '$columnId = ?', whereArgs: [id]); } |
Update
1 2 3 |
Future<int> update(Model model) async { return await db.update(table, model.toMap(), where: '$columnId = ?', whereArgs: [model.id]); } |
References: