31.5. Data Persistence with SQLite in Flutter: Data Query

Página 259

SQLite is an embedded SQL database engine that does not require a separate server process. It is widely used in mobile devices and small applications, making it an excellent choice for data persistence in Flutter applications. In this chapter, we'll explore how to query data using SQLite in Flutter.

Before we get started, it's important to understand that SQLite stores data in tables, which are collections of rows. Each row represents a single record or data item, and each column in a row represents a field of data. To query data in SQLite, we use SQL (Structured Query Language), which is a standard language for interacting with relational databases.

First, we need to configure our Flutter application to use SQLite. This involves adding the 'sqflite' dependency to our 'pubspec.yaml' file. Next, we import the sqflite package into our Dart file. We also need to import the 'path_provider' package, which helps us find the path to the directory where we are going to store our database.

dependencies:
  flutter:
    sdk: flutter
  sqflite: ^1.3.0
  path_provider: ^1.6.22

After importing the necessary packages, we can create our database. This is done by calling the 'openDatabase' function and providing a path to the database file. If the database does not exist, it will be created. Otherwise, it will open.

final Future database = openDatabase(
  join(await getDatabasesPath(), 'my_database.db'),
);

Now that we have our database, we can create a table to store our data. This is done using the 'execute' function to execute a CREATE TABLE SQL statement. This statement defines the table name and data types for each column.

await database.execute(
  "CREATE TABLE my_table(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",
);

With our table created, we can insert data into it. This is done using the 'insert' function, which takes the table name and a map of the data to be inserted. The map must have a key for each column in the table and a corresponding value for each key.

await database.insert(
  'my_table',
  {'id': 1, 'name': 'John Doe', 'age': 30},
);

Finally, we come to the important part: querying the data. This is done using the 'query' function, which takes the name of the table and returns a list of maps, where each map represents a row in the table. We can then iterate over this list to access the individual data.

final List> maps = await database.query('my_table');

We can also use the WHERE clause in our query to filter the results. For example, we can fetch all rows where age is greater than 25.

final List> maps = await database.query(
  'my_table',
  where: 'age > ?',
  whereArgs: [25],
);

SQLite in Flutter is a powerful tool for data persistence. With it, we can create, enter and query data efficiently and effectively. However, it's important to remember that SQLite is just one of the many options available for persisting data in Flutter. Depending on your application's needs, you may find that other solutions such as Firebase's Firestore or even a custom REST API are more appropriate.

I hope this chapter has given you a good overview of how to query data using SQLite in Flutter. In the next chapter, we'll explore how to update and delete data.

Now answer the exercise about the content:

What is the purpose of 'openDatabase' function in SQLite in Flutter?

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

You missed! Try again.

Next page of the Free Ebook:

26031.6. Data Persistence with SQLite in Flutter: Data Refresh

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