31.6. Data Persistence with SQLite in Flutter: Data Refresh

Página 260

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.

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.

Next page of the Free Ebook:

26131.7. Data Persistence with SQLite in Flutter: Data Deletion

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text