Persist data with SQLite

Tram Ho

Preamble

When writing a moblie app you often find that you need to save data between app launches. If the data is simple enough as some app settings, login information, .. you can save them as key-values ​​in sharedPreferences. But when the data needs to be more complex and interdependent, we will need to use a more formal data storage system – the database. Database generally provides faster insertion, update and query than other local storage methods (file, sharedpreferences, …).

Like native Android or IOS programming, Flutter also uses SQlite – the most popular option for database management. In this article, I will show you how to manage database with spflite plugin. Sqflite is a popular plugin and is introduced on flutter’s document so you can safely use it

A few things about SQFLite

  • SQFLite supports iOS, Android and MacOS, does not support web platform.
  • Support transctions and batches.
  • Automatic version management
  • There is a helper for insert, query, update, delete queries
  • DB operation is performed in the background thread on iOS and Android

Data types supported by sqlite:

  • Integer: Dart type – int
  • Real: Dart type – num
  • Text: Dart type – String
  • Blob: Dart type – Uint8List

Unsupported DateTime -> can be saved as millisSinceEpoch or String.

Bool is not supported -> use a different type instead, for example 0 or 1

For example

We will go into a specific example to understand it more easily. In this example we will save the information of a movie model to the database when we press favorite.

First we need

Add the necessary dependencies to the pubspec.yaml file

Located in the Movie class, specify the information to be stored. Here we will store id, porterPath, title, overview, releaseDate, voteAverage. Where id is a unique field, we will set as a primary key.

Open the database

Before reading and writing data to the database, we need to open a connection:

  • Determine the path to the database using getDatabasePath () from the sqflite package, combined with the join function from the package path .
  • Open the database with the openDatabase () function of sqflite.

Create a “favorite” Table

Next, create a table that stores information about favorite Movie.

  • id type int -> INTEGER
  • porterPath, title, overview, releaseDate of String type -> TEXT
  • voteAverage type double -> REAL

Note: the column name should be the same as the key in the row toMap (), formJson () which makes reading and inserting data easier.

Insert a Movie into the DB

The movie is converted to Map () and saved to the TABLE_FAVORITE table, with the key being the name of the column value, which will be the value filled in that column.

You can also specify conflictAlgorithm to use in case the same movie is inserted twice.

Get the saved List Movie

The query db.query(TABLE_FAVORITE) will return the list of records in the DB, each record will be a Map with the key being the column name, the value is the data of that column. What we need to do then is just convert to List <Movie> and use it.

Movie Updata is already in the table

The update record has id == movie.id with the new data movie.toMap ().

Delete movie

Delete record has id == movie.id

Close the DB

Normally the DB will be closed when you turn off the app. However, if you want to release resources, you can close the DB by

Conclude

Above I just presented a simple way of using SQLite in a Flutter application. The code of the above example section you can see the details here .

If the above is too simple, you want to learn more, you can play with the pagkage sqflite here.

Thank you for watching the article !!

Share the news now

Source : Viblo