Sqflite
Sqflite is a lightweight and easy-to-use database plugin for Flutter applications. It provides a simple interface for developers to perform CRUD (Create, Read, Update, and Delete) operations on SQLite databases. Flutter is a cross-platform mobile development framework that allows developers to build high-performance mobile applications for both Android and iOS platforms. In this blog, we will discuss how to use Sqflite in Flutter to create and manipulate SQLite databases.
Getting started with Sqflite in Flutter
To use Sqflite in Flutter, we need to add the sqflite package to our Flutter project. We can add it to our pubspec.yaml file as follows:
dependencies:
sqflite: ^2.0.0+3
After adding the package, we can import it into our Dart code as follows:
import 'package:sqflite/sqflite.dart';
Creating a database
To create a database, we need to specify its name and version number. We can use the openDatabase()
method to create and open a database. Here is an example of creating a database:
final database = await openDatabase(
'my_database.db',
version: 1,
onCreate: (db, version) {
db.execute(
'CREATE TABLE contacts(id INTEGER PRIMARY KEY, name TEXT, email TEXT)',
);
},
);
In the above code, we created a database named my_database.db
with the version number 1
. We also defined the database schema using SQL commands in the onCreate
callback function. The db.execute()
method is used to execute SQL commands.
Performing CRUD operations
Once we have created a database, we can perform CRUD operations on it. Here are some examples:
- Inserting data into the database
To insert data into the database, we can use the insert()
method. Here is an example:
await database.insert(
'contacts',
{'name': 'Raman', 'email': 'example@example.com'},
);
In the above code, we inserted a new contact into the contacts
table.
- Updating data in the database
To update data in the database, we can use the update()
method. Here is an example:
await database.update(
'contacts',
{'email': 'example@example.com'},
where: 'name = ?',
whereArgs: ['Raman'],
);
In the above code, we updated the email address of the contact named Jane Doe
.
- Querying data from the database
To query data from the database, we can use the query()
method. Here is an example:
final result = await database.query(
'contacts',
where: 'name = ?',
whereArgs: ['John Doe'],
);
In the above code, we queried the database for the contact named John Doe
.
- Deleting data from the database
To delete data from the database, we can use the delete()
method. Here is an example:
await database.delete(
'contacts',
where: 'name = ?',
whereArgs: ['John Doe'],
);
In the above code, we deleted the contact named John Doe
.
Closing the database
After we have finished using the database, we should close it to free up resources. We can use the close()
method to close the database. Here is an example:
await database.close();
In the above code, we closed the database.
Conclusion
In conclusion, Sqflite is a powerful and user-friendly database plugin for Flutter applications. It enables developers to perform CRUD operations on SQLite databases with ease. Sqflite is highly customizable and can be used to build complex database schemas. Additionally, it is lightweight and fast, making it ideal for mobile applications that need to store data locally. With Sqflite, Flutter developers can build efficient and scalable mobile applications that provide an excellent user experience. Overall, Sqflite is a valuable tool that any Flutter developer should consider when building mobile applications that require local data storage.