Lets build an app: Part 25 — integrating databases in your app

Jayanth Srinivasan
5 min readDec 1, 2022

--

Today, we will be looking into the different types of databases that you can make use of in your applications and how to integrate them as well!

Basically, there are three types of databases that you can use in your applications and they are: Cloud, SQL (relational) and No-SQL databases!

Photo by Pawel Czerwinski on Unsplash

So, what does this mean? SQL or relational databases is a database which stores some information in tables and you can retrieve that data and that is exactly what you can implement with a relational database.

No-SQL database isn’t relational and the user doesn’t need to do queries like in the case of a SQL database. A well known example of a No-SQL database is Firebase which is offered by Google and with it, you can easily perform some database operations in your app. But today we will be focusing on relational databases with SQL.

In order to do that we have something called as Sqflite for integrating a SQL database in your Flutter project. So, lets look into the steps now:-

Lets take an example where you have to store a value in your app for the time being. The value is of the type — integer. So, what do you need to do? Now create a new Flutter project in Android Studio or open up an existing one…

Today, we will just be looking into the basic operations that you can make with Sqflite and move forward gradually in the upcoming parts! Some of the basic operations that you can perform with Sqflite is simple: table and database creation, inserting values into the table, updating the values and deleting the values.

How to connect to a database and create one? First things first, import the necessary packages:

Sqflite is used to integrate SQL in your app and the “path” package is used to get the path or directory of the file where you want to store your data or information!

Now, to open a database:-

Here, we have created a variable database that opens the specified database in the “openDatabase” function. “join” is used to get the correct path of the place where the specified file should be stored, “getDatabasesPath()” gets the files directory or path.

Next up, we will create a table for the database and the values of this table are — num (the number) to be stored in the database. For this we will be using the function “onCreate” and this function accepts two parameters: the database and the version of the database. Lets take a look into the code now:-

“db.execute” is used to execute a query of the database and for this we have to mention the table name and pass the values of the table in the execute command. And then we are passing the version number of the database. The database name is “number” and the values that we are passing is the number specified as “num” and the type as “INTEGER”. “num” is the primary key of the table which is used to uniquely identify values in the table!

To perform insert and update operation on the table you need to create a basic table class:-

Let the name of the class be Numb and the value that it will hold is the number that we have to store in the database. The class will look like this:-

Numb is the name of the class and its parameter is the number. You will notice something strange in the instance of the class and that is the “required” keyword, so what is it? It simply means that this variable is a required one to be inserted or to be passed in the class when this class is called in the app somewhere and without the variable, the table wont store any further data and the table wont get created!

Now, to perform the insert and update operations we cant call the class and perform these operations and the passed data has to get stored in the table in the form of a list/map and in order to do that, we pass this in the class “Numb”:-

“toMap” is the function name and we return the content of the table which is the number. “Map” is used to mention that the data should be of the type integer.

Next, we will look how to insert the contents in the table:-

“insertDog” is the name of the function and what we are doing here is, we get the instance of the database and then we call the function “db.insert” which is used for inserting the content in the table and then we pass the table name. You can find the use of “conflictAlgorithm” in the image itself.

Now, how to display the contents of your table?

And that’s it, for the retrieving part of the table/database, just go through the code snippet and understand it and in case you don’t worry, you can always just use this in your apps! We will be looking into some more concepts of database integration in the upcoming parts of the series!

Well, that’s it for today then, ciao 😴😴😴😶‍🌫️

--

--