Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
Practical Paper
Industrial Training
Flutter Database
A database is an organized collection of data, which supports the storage and manipulation of data and accessed electronically from a computer system. We can organize data into rows, columns, tables, and indexes. It makes data management easy. We can store many things in the database, like name, age, picture, image, file, pdf, etc.
Flutter provides many packages to work with the database. The most used and popular packages are:
- sqflite database: It allows to access and manipulate SQLite database.
- Firebase database: It will enable you to access and manipulate the cloud database.
SQLite Database
SQLite is a popular database software library that provides a relational database management system for local/client storage. It is a light-weight and time-tested database engine and contains features like self-contained, server-less, zero-configuration, transactional SQL database engine.
Flutter SDK does not support SQLite directly. Instead, it provides a plugin sqflite, which performs all operations on the database as similar to the SQLite library. The sqflite provides most of the core functionalities related to the database are as follows:
- It creates or opens the SQLite database.
- It can execute SQL statements easily.
- It also provides an advanced query method to get information from the SQLite database.
Let us see step by step how we can store and fetch data in the Flutter.
Step 1: First, create a new project in Android Studio and add the dependencies in pubspec.yaml file.
dependencies: flutter: sdk: flutter sqflite: any path_provider: any
- The sqflite package provides classes and functions to interact with the SQLite database.
- The path_provider package provides functions to define the location of your database on the local system, such as TemporaryDirectory and ApplicationDocumentsDirectory.
Step 2: Create a model class. In this step, we have to define the data that needs to be stored before creating a table to store information. The following code explains it easily.
class Book { final int id; final String title; final int price; Book({this.id, this.title, this.price}); }
Step 3: Open the database. Here, we need to open the connection to the database. It requires two steps:
- Set the path to the database by using the getDtabasePath() method and combined it with the path package.
- Use openDatabase() function to open the database.
// It allows to open the database and store the reference. final Future< Database> database = openDatabase( join(await getDatabasesPath(), 'book_database.db'), );
Step 4: Create the table. In this step, we have to create a table that stores information about the books. Here, we are going to create a table named books, which contains the id, title, and price of the books. They are represented as three columns in the book table.
final Futuredatabase = openDatabase( join(await getDatabasesPath(), 'book_database.db'), // When you create a database, it also needs to create a table to store books. onCreate: (db, version) { // Run the CREATE TABLE statement. return db.execute( "CREATE TABLE books(id INTEGER PRIMARY KEY, title TEXT, price INTEGER)", ); }, // Set the version to perform database upgrades and downgrades. version: 1, );
Step 5: Insert a Book into the database. Here, you have to store information on the table about the various books. Inserting a book into the table involves two steps:
- Convert the Book into a Map
- Uses insert() method
The following code explains it more clearly.
// Update the Book class. class Book{ final int id, final String title; final int price; Book({this.id, this.title, this.price}); // It converts a Book into a Map. The keys correspond to the names of the columns in the database. MaptoMap() { return { 'id': id, 'title': title, 'price': price, }; } } Future< void> insertBook(Book book) async { final Database db = await database; await db.insert( 'books', book.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } // Create a Book and add it to the books table. final b1 = Book( id: 0, title: 'Let Us C'., price: 350, ); await insertBook(b1);
Step 6: Retrieve the list of books. Now, we have stored the book into the database, and you can use a query to retrieve a particular book or list of all books. It involves two steps:
- Run a query that returns List
- Convert the List
You can see the following code to understand it easily.
// This method retrieves all the books from the books table. Future> books() async { final Database db = await database; // Use query for all Books. final List< Map< String, dynamic>> maps = await db.query('maps'); return List.generate(maps.length, (i) { return Book( id: maps[i]['id'], title: maps[i]['title'], price: maps[i]['price'], ); }); } // It prints all the books. print(await books());
Step 7: Update a Book in the database. You can use an update() method to update the book that you want. It involves two steps:
- Convert Book into the Map.
- Then, use where clause to update the book.
You can see the following code to understand it.
FutureupdateBook(Book book) async { final db = await database; // Update the given Book. await db.update( 'books', book.toMap(), // It ensure the matching id of a Book. where: "id = ?", whereArgs: [book.id], ); } // Update b1 price. await updateBook(Book( id: 0, title: 'Let Us C', price: 420, )); // Print the updated results. print(await books());
Step 8: Delete a Book from the database. You can use the delete() method to delete the database. For this, you need to make a function that takes an id and delete the database of the matching id.
FuturedeleteBook(int id) async { final db = await database; // This function removes books from the database. await db.delete( 'books', where: "id = ?", whereArgs: [id], ); }
Let us see the complete code to understand how we can create a database file with sqflite plugin. For this, create a new Flutter project and the sqflite and path package into the pubspec.yaml file. Next, create a new file into the lib folder and insert the following code in this file. Now, connect the database with your UI and run the code.
import 'dart:async'; import 'packprice:path/path.dart'; import 'packprice:sqflite/sqflite.dart'; void main() async { final database = openDatabase( join(await getDatabasesPath(), 'book_database.db'), onCreate: (db, version) { return db.execute( "CREATE TABLE books(id INTEGER PRIMARY KEY, title TEXT, price INTEGER)", ); }, version: 1, ); FutureinsertBook(Book book) async { // Get a reference to the database. final Database db = await database; await db.insert( 'books', book.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } Future< List< Book>> books() async { final Database db = await database; final List< Map < String, dynamic>> maps = await db.query('books'); return List.generate(maps.length, (i) { return Book( id: maps[i]['id'], title: maps[i]['title'], price: maps[i]['price'], ); }); } Future updateBook(Book book) async { final db = await database; await db.update( 'books', book.toMap(), where: "id = ?", whereArgs: [book.id], ); } Future deleteBook(int id) async { final db = await database; await db.delete( 'books', where: "id = ?", whereArgs: [id], ); } var b1 = Book( id: 0, title: 'Let Us C', price: 300, ); await insertBook(b1); print(await books()); b1 = Book( id: b1.id, title: b1.title, price: b1.price, ); await updateBook(b1); print(await books()); await deleteBook(b1.id); print(await books()); } class Book { final int id; final String title; final int price; Book({this.id, this.title, this.price}); Map< String, dynamic> toMap() { return { 'id': id, 'title': title, 'price': price, }; } @override String toString() { return 'Book{id: $id, title: $title, price: $price}'; } }