Ionic 5 Angular SQLite database tutorial; throughout this step-by-step guide, you will find out how to create SQLite CRUD operation in the Ionic Angular app with the SQLite package’s help.
Ideally, SQLite is robust database exclusively used to create database system in smaller devices which allow us to perform SQL queries and build a simple app with CRUD operations.
Generically, in this quick guide, we will demonstrate how to create an SQLite database, create a table and insert into device database, add or insert data into the table, fetch, get or read data from the table, delete a row from the table, and update the row from the table in ionic sqlite offline mobile app.
Ionic 5 SQLite Offline CRUD Mobile App Example
- Step 1: Configure Ionic Environment
- Step 2: Update Routes
- Step 3: Install SQLite Plugin
- Step 4: Update App Module Class
- Step 6: Create SQLite CRUD Service
- Step 7: Implement Create and Delete
- Step 8: Get Data Collection and Display List
- Step 9: Implement Update or Edit
- Step 10: Test Ionic Crud App
Configure Ionic Environment
Ionic CLI is a must-have tool; therefore, begin with installing it. Run the command to launch the installation process:
Next, install the new blank ionic angular app:
Head over to project folder:
Next, generate ionic pages or components with the help of the suggested command. Ensure to delete the Home page because we don’t need that.
Update Routes
Next, head over to the app routing file; add the id property in the edit route considerably set the create as a default route.
Update app-routing.module.ts file:
Install SQLite Plugin
This is the most quintessential step; as per the instruction, we need to install the SQLite plus Ionic native core plugins.
Open the terminal, type command and then execute.
Update App Module Class
Further, we need to import the SQLite plugin into the app module class, and it gives us access to its methods and functions to use throughout the app.
Update app.module.ts file:
Create SQLite CRUD Service
Angular service is better for code manageability and reusability paradigm. Consequently, generate the service for ionic offline crud mobile app using the following command.
In the next step, we have to create and add, read, update and delete methods. Most importantly, we have to invoke the SQLite database connection in the Ionic app from the ionic, angular service file.
The sqlite offers create method, which takes database name and location and initializes the SQLite database connection.
Above command manifested crud.service.ts file, open it and update the suggested code within the file.
We have to perform multiple tasks using service file, first import Platform, SQLite and SQLiteObject modules. These are essential for creating and setting up the SQLite database connection when the platform is ready.
To handle CRUD operations we are using SQLite, that allows writing MYSql queries with executeSql() method. Moreover, we created SQLite database connection, created database, created and inserted table into the database. Also, addItem(), getAllUsers(), getUser(), updateUser() and deleteUser() for handling CRUD events.
Implement Create and Delete
In this step, you we will learn to implement how to create and display list feature in ionic page using the SQLite.
In this step, we will add a basic form to let users enter data such as name and email using ngModel. Use CrudService by importing and adding into the constructor method, access methods creating user, fetching users list from the SQLite database and deleting user object from the SQLite db.
Add code in create.page.ts file:
To display the data collection, use the ngFor directive to iterate over the USERS collection and show it into the ionic item UI component.
Add code in create.page.html file:
Implement Update or Edit
Lastly, we will implement the edit or update feature so make sure to create the form and insert the blog values by using the angular service.
Additionally, we used the ActivatedRoute api to get the user id from the url, and it is being passed to the getUser() method. This is getting the single user object from the SQLite database, likewise using the updateUser() method to update the user data.
Update edit.page.ts file:
Open and update edit.page.html file:
Test Ionic Crud App
In the eventual step, you need to follow the recommended instructions to start the ionic crud example app:
Include the platform:
Thereafter, create the runnable build:
Ultimately, run the app on the device:

Conclusion
So this was it; the Ionic 5 SQLite CRUD mobile app tutorial is done for now; In this example, we described how to create an Ionic CRUD mobile app using SQLite database plugins. Implementing the SQLite database in the Ionic app gives easy access to SQLite database methods that we can use to store and manage the data for building offline mobile apps.
Not just that, we also comprehended the crud app building process step-by-step. That consists of creating an app, installing plugins, setting up plugins to access database methods, creating the angular service for reusable code, and implementing the crud operations in ionic pages.
Download the project code from GitHub.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.