Ionic 4 | Adding Native Storage in Application Using Cordova SQLite Storage Plugin

In this post, we will add Cordova’s SQLite storage plugin in Ionic version 4 Application. This is the common and widely used storage plugin by most of the Ionic developers since version 1 of Ionic. Storage is widely used to store personalized settings by the user in the application which is not required to be stored on the server side.

Application developers sometimes need to save a bunch of information on the user side to control some features and provide some offline functionalities to application users, in that case, local storage proves very handy. Using Ionic’s Native storage plugin we can storage key value pairs which may have any type of data from a simple string or integers to JSON objects. But this locally stored data will remain until the application is Uninstalled or device is formatted.

LocalStorage engines are managed by the plugin itself according to OS on which application is installed or running like in Native app context Storage will be done using SQLite, which is one of the most stable and most preferred file-based storage databases. On Progressive Web Apps platform IndevedDB, WebSQL or localStorage is chosen in a defined order.

Also, Check:

Ionic 4 | Storage Tutorial in Ionic using Native Storage Plugin

Complete SQLite CRUD Tutorial for Ionic 4 Angular Applications

 

Let’s start the implementation of Storage Plugin in Ionic 4 Application

Here we will start with a new application in Ionic version 4 which is now in beta phase but soon stable version will be available with minor installation step changes in commands to install.

Create a new Ionic 4 Application

Create a new application using Ionic CLI
To create an application you must have NodeJS and Ionic Cordova CLI

 

Run the following command to create a new application

 

Install Cordova and Ionic Native Plugin

 

Import declare plugin in Application’s module file

In the app.module.ts file, import plugin then adds in Imports array in NgModule. Replace below code:

 

After adding the plugin to the app’s main module, we can now use Native Storage anywhere in the application. Here we will use it in the home component.

In the home.page.ts file, import plugin then creates an instance in component’s constructor.

In the above code, we just simply setting and getting the string value.

 

Set a Value in a Key

set() : Set the value for the given key. Returns a promise that resolves when the key and value are set

 

Get Value in a Key

get() : Get the value associated with the given key. Returns a promise with the value of the given key

 

Remove a key/value pair

remove() : Remove any value associated with this key. Returns a promise that resolves when the value is removed

 

Current Storage Engine Used

Get the name of the driver being used.

Traverse key/value pairs

forEach() : Iterate through each key,value pair. Returns a promise that resolves when the iteration has finished.

 

All Stored Keys

keys() : Returns a promise that resolves with the keys in the store.

 

Total Keys Stored

length() : Returns a promise that resolves with the number of keys stored.

You can check more options here

Finally out component will look like this

To call these methods, add below HTML in home.page.html

Here we discussed How we can Integrate Cordova’ Storage Native plugin in Ionic 4 Application to Store Values in different formats and retrieve them as per application requirements.

Thanks for visiting :) Let me know if you have some suggestions or feedback for this post in the comment section. Looking for any tutorial or demo on FreakyJolly, just drop me a message here

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
code android example Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
code android example
Guest

i want you code demo for me project sqlite.