0% found this document useful (0 votes)
41 views

4-Flutter SQLite

The document provides a guide on using SQLite for local data storage in Flutter applications, emphasizing the use of the sqflite plugin for database operations. It outlines the steps to set up SQLite, including adding dependencies, opening a database, creating tables, and performing CRUD operations. Key methods and code examples are provided to illustrate how to create a database, define tables, and execute SQL commands for data manipulation.

Uploaded by

kh355406fm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

4-Flutter SQLite

The document provides a guide on using SQLite for local data storage in Flutter applications, emphasizing the use of the sqflite plugin for database operations. It outlines the steps to set up SQLite, including adding dependencies, opening a database, creating tables, and performing CRUD operations. Key methods and code examples are provided to illustrate how to create a database, define tables, and execute SQL commands for data manipulation.

Uploaded by

kh355406fm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Flutter SQLite:

Whenever you need to store the data on the local then you need to
use SQLite to persist the user data.

Why we use SQLite?

SQLite is a popular choice as embedded database software for


local/client storage in application software such as web browsers.

How to use SQLite in a flutter?

Before using SQLite, you should know that

SQLite is not available in a flutter SDK like Android but we have


a plugin sqflite that exactly performs all the operation on the
database just like in Android and iOS.
Implementation

Step 1: Add the dependencies

Add dependencies to pubspec.yaml file .

sqflite:
path_provider:

To work with SQLite databases, import


the sqflite and path packages

• The sqflite package provides classes and functions that


allow you to interact with a SQLite database.

• The path_provider package provides functions that allow


you to correctly define the location to store the database on
disk such getDAtaBasePath method
and ApplicationDocumentsDirectory.

Step 2: Open the Database

Before you read and write data to the database, you need to open a
connection to the database. This involves two steps:

1. Define the path to the database file using


the getDatabasesPath from the sqflite package
combined with the pathfunction from the path package
2. Open the database with the openDatabase function
from Sqflite
openDatabase functionhas some paramaters
should you know it before use it.
• Path: which represent the path where the
database must be created, actually the path
should be in same path of your app directory.
• Version: this parameter is integer datatype
which represent the version of database, we
use it to differentiate between the versions
of same database if we want to make any
changes in the structure of database.
openDatabase Methodes: there are 5
methods(onCreate,
onUpgrade,onConfigure,onDownground,onOpen)
here we will tlak about the first tow as
follow:
1. onCreate: this method called when we want
to create a table in the Database, it
needs two parameters(Database and
version), for your information when we
create a table using onCreate method we
can not make any changes in structure of
that table unless we change the version
and use onUpgrade method which is next .
2. onUpgrade: this method called only when we
want to make any changes in table
structure, like add a new field change
field datatype and so, but to do this
process we have to change the version of
database, which means we have to create a
new version of that Database with a new
version.
Here is the code:
intialDB() async {
String dbPath = await getDatabasesPath();
String path = join(dbPath, 'mydb.db');
Database mydb = await openDatabase(path,
version : 1, onCreate: _onCreate);
return mydb;
}

Step 3: Create the table

you need to create a table to store information.


For example, In our case, we create a table called Person that defines
the data that can be stored. In this case, each Person contains
an id, name, and city. Therefore, these will be represented as three
columns in the Person table.

1. The id is a Dart int, and will be stored as


an INTEGER SQLite Datatype. It is also good practice to use
an id as the primary key for the table to improve query and
update times.

2. The name is a Dart String, and will be stored as


a TEXT SQLite Datatype

3. The city is also a Dart String, and will be stored as


an TEXT Datatype
and for that we use onCreate method as follow:

_onCreate(Database db, int version) async {


await db.execute('''
CREATE TABLE "Person"(
"id" INTEGER AUTOINCREMENT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"city" TEXT NOT NULL,
)

''');
return ("Database
Created============================");
}

Step 4 : Create the The crud operation in Sqflite

The most important operations must done on table is insert data, update
data, delete data, and any special Queries as per user needs.
Now we are going to show you how you can perform an operation on the
SQLite database.
//Read Data From Database
readData(String sql) async {
Database? mydb = await db;
List<Map> response = await mydb!.rawQuery(sql);
return response;
}

// insert data into database


insertData(String sql) async {
Database? mydb = await db;
int response = await mydb!.rawInsert(sql);
return response;
}
// delete data from database

deletetData(String sql) async {


Database? mydb = await db;
int response = await mydb!.rawDelete(sql);
return response;
}

//Update Data From Database

updatetData(String sql) async {


Database? mydb = await db;
int response = await mydb!.rawUpdate(sql);
return response;
}

You might also like