Data Persistence with SQLite in Flutter: Data Refresh

Capítulo 260

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Data persistence is one of the fundamental parts of application development. In Flutter, one of the most common ways to persist data is to use SQLite, a lightweight and efficient database that can be easily incorporated into an application. In this module, we are going to discuss how to update data in an SQLite database using Flutter and Dart.

SQLite is a relational database that allows you to efficiently store, retrieve, update, and delete data. It is widely used in mobile applications due to its lightness and simplicity. To use SQLite in Flutter, we need to use a package called sqflite.

Installing sqflite

To begin with, we need to add sqflite to our project. We can do this by adding the following line to our pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  sqflite: ^2.0.0+3

Now, we can import sqflite into our Dart file:

import 'package:sqflite/sqflite.dart';

Updating data in SQLite

To update data in SQLite, we first need to open a connection to the database. We can do this using the openDatabase method. This method returns a Database object that we can use to interact with the database.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

Next, we need to create an SQL UPDATE statement to update the data. The UPDATE statement modifies the values ​​of one or more columns in one or more rows in a table. The basic syntax of an UPDATE statement in SQLite is as follows:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

In Flutter, we can execute an SQL statement using the rawUpdate method of the Database object. For example, to update a user's age, we can do the following:

int count = await db.rawUpdate(
    'UPDATE User SET age = ? WHERE name = ?',
    ['new_age', 'old_name']
);

This code updates the 'age' column to 'new_age' for all rows where the 'name' column equals 'old_name'. The rawUpdate method returns the number of rows affected by the update.

We can also use the Database object's update method to update data. This method accepts the table name and a column value map. For example:

int count = await db.update(
    'User',
    {'age': 'new_age'},
    where: 'name = ?',
    whereArgs: ['old_name']
);

This code does the same thing as the previous example, but uses the update method instead of rawUpdate. The update method is safer and easier to use than rawUpdate as it automatically escapes values ​​and avoids SQL injection.

Conclusion

Updating data in an SQLite database in Flutter is a simple and straightforward task. With the sqflite package, we can easily run SQL statements to update the data. Remember to always close the database connection when you're done using it to avoid memory leaks.

In the next module, we will discuss how to delete data from an SQLite database in Flutter. Stay tuned!

Now answer the exercise about the content:

What is the difference between rawUpdate and update methods of Database object in Flutter?

You are right! Congratulations, now go to the next page

You missed! Try again.

The update method is safer and easier to use because it automatically escapes values and avoids SQL injection risks, unlike the rawUpdate method which requires manual SQL statement handling.

Next chapter

Data Persistence with SQLite in Flutter: Data Deletion

Arrow Right Icon
Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course
97%

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.