What is sqflite?
- Flutter course
- Intermediate
- Lesson 41/99
- 4 min
Open a local SQLite database with sqflite.
What is sqflite?
sqflite is a package used for local SQLite database operations in Flutter. You create tables, insert records and query with SQL.
import 'package:sqflite/sqflite.dart';
final db = await openDatabase('app.db');sqflite works async; all operations return a Future.
openDatabase
The openDatabase method opens a database file, or creates it if it does not exist. The return value is a Database object; all queries are done through this object.
final db = await openDatabase(
'app.db',
version: 1,
);The database name usually has a .db extension.
Practice Open database and print ready
Write an async method named ac; import the sqflite package, get db with await openDatabase('uygulama.db') and call print('veritabanı hazır'). In main, await ac.
Starter code
import 'package:sqflite/sqflite.dart';
// ac: async metot ekle
void main() async {
// ac metodunu çağır
}Sample solution
import 'package:sqflite/sqflite.dart';
Future<void> ac() async {
final db = await openDatabase('uygulama.db');
print('veritabanı hazır');
}
void main() async {
await ac();
}Expected output
veritabanı hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What kind of data is sqflite suitable for?
- Only bool preferences
- Only image files
- Only a single String value
- Structured relational data Correct answer
Explanation: sqflite is an SQL-based relational database; it stores structured data in tables and queries them.
2 Import the sqflite package
import 'package:sqflite/____.dart';
import 'package:sqflite/sqflite.dart';
Explanation: The sqflite package import path is 'package:sqflite/sqflite.dart'.
3 sqflite parts
| Term | Meaning |
|---|---|
sqflite |
SQLite database package |
openDatabase |
Method opening the database |
Database |
Object where queries run |
SQL |
Query language for tables |