Using SqlStorage Instead Of SQLite In An Ionic 2 App

When it comes to Ionic 2 there are many ways that you can store your data. For example you could use HTML5 local storage, Mozilla’s localForage library, or Ionic’s SQLite extension that is part of Ionic Native. With these options available, I get a lot of requests for information on Ionic’s less advertised SqlStorage option.

We’re going to take a look at using SqlStorage in an Android and iOS application rather than the SQLite alternative.

Before getting too deep into this we should probably figure out the differences between SQLite in Ionic Native and SqlStorage which is part of the Ionic platform. Per the Ionic 2 documentation, SqlStorage will use the Apache Cordova SQLite plugin, if available, otherwise it will fall back to WebSQL. This is different from Ionic Native SQLite which will only use the Apache Cordova SQLite plugin. I previously wrote how to use SQLite with Ionic Native.

To show SqlStorage in action, we’re going to create a fresh Ionic 2 project. From the Command Prompt (Windows) or Terminal (Mac and Linux), execute the following:

There are a few things to note in the above set of commands. First you’ll note the --v2 tag. This means we are creating an Ionic 2 project which requires the Ionic 2 CLI. We are adding both the iOS and Android platforms, but if you’re not using a Mac, you won’t be able to build for iOS.

With the project created, we have an optional next step. If you wish to use the Apache Cordova SQLite plugin, which you definitely should, then you need to install it. This can be done by executing the following:

ionic plugin add cordova-sqlite-storage

Remember, if you don’t install the plugin, you’ll be using WebSQL which has a few limitations in comparison to the native alternative.

We’re going to edit two different files in our Ionic 2 project. Starting with the TypeScript logic, open the project’s app/pages/home/home.ts file and include the following code:

There is a lot happening in the above TypeScript file, so it is probably best that we break it down.

To make this project possible we need to import both the SqlStorage and Storage components that are part of the Ionic 2 platform. Inside the HomePage class we define both a public and private variable. The personList array is public because it will be accessed via the UI.

In the above constructor method we initialize our storage object and create a new database table if it doesn’t already exist. We are also initializing our personList to a blank array.

public onPageLoaded() {
this.refresh();
}

After initializing all our components we find ourself in the onPageLoaded method. This method is triggered after the constructor method fires. This method is responsible loading our data. Now it makes sense to look ahead to the refresh method that we’re trying to use.

In the above method we are inserting data into the database. Of course this example is very simple so we’re not inserting dynamic data. Every insert will only add my name to the database. After the insert is complete, the data will be pushed into the array of objects for displaying on the screen.

This brings us to the UI of our simple application. Open the project’s app/pages/home/home.html file and include the following markup:

In our HTML file we essentially have two buttons in the header. One button will refresh the list and the other will add items to the list. The core content of this HTML file is a loop of our array of objects.

Conclusion

You just saw how to use SqlStorage instead of SQLite with Ionic Native. While I haven’t really figured out why you should use one over the other when both interface with the same Apache Cordova SQLite plugin, at least you have options.

If you want to take this to the next level, you should try to create a shared service from the SqlStorage code. This can help reduce a lot of duplicate code. If you need help getting started with this, check out a similar tutorial I wrote on the subject.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.