CodeBlanks Logo Icon CodeBlanks

What is sqflite?

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

main.dart
import 'package:sqflite/sqflite.dart';

// ac: async metot ekle

void main() async {
  // ac metodunu çağır
}

Sample solution

main.dart
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

Question
import 'package:sqflite/____.dart';
Answer
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

All lessons in Intermediate