How to use SQLite/Sqflite CRUD on the Flutter App

Today we will see how to perform a CRUD Operation in Flutter with SQLite.We are going to use an embedded SQLite database engine to store databases in Flutter Android and iOS.Before going ahead make sure you have at least basic knowledge of Flutter. How to use SQLite/Sqflite CRUD on the Flutter App. Download flutter from

First, you need to add full path and sqflite flutter packages to your dependency by adding the following lines on pubspec.yaml file.

    sdk: flutter
  path: ^1.8.0
  sqflite: ^2.0.0+4

Now we will see how to initialize and Create Database

// Get a location using getDatabasesPath
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
//join is from path package
print(path); //output /data/user/0/com.testapp.flutter.testapp/databases/demo.db
Database db = await openDatabase(path, version: 1,
    onCreate: (Database db, int version) async {
      // When creating the db, create the table
      await db.execute('''
            CREATE TABLE IF NOT EXISTS students( 
                  id primary key,
                  name varchar(255) not null,
                  roll_no int not null,
                  address varchar(255) not null
              //create more table here
    //table students will be created if there is no table 'students'
    print("Table Created");
Now add new tables in our database 
db.rawInsert("INSERT INTO students (name, roll_no, address) VALUES (?, ?, ?);", ["Dheeru", 12, "India"]); 
//add student from form to database

Now check how to get data or records from our database

List<Map> slist = await db.rawQuery('SELECT * FROM students');{

Now fetch selected data using the select method and we will use the map() method for fetching the data .

List<Map> slist = await db.rawQuery('SELECT * FROM students WHERE roll_no = ?', [12]);
if(slist.length > 0){
   var data = slist.first;
  print("NO any student with roll no 12");

Now we will see update code for our flutter project .

db.rawInsert("UPDATE students SET name = ?, roll_no = ?, address = ? WHERE roll_no = ?", ["New Name", 23, "New Address",12]);
//update students table with roll_no = 12

Now we will try to delete the row with id

db.rawDelete("DELETE FROM students WHERE roll_no = ?", [12]);
//delete student data with roll no = 12

This way we can perform a CRUD operation in FLUTTER .

Leave a Reply

Your email address will not be published. Required fields are marked *