Ionic 2 is the upgraded version of ionic 1 and it has a lot of differences in it than the earlier one. Typescripts are introduced in this as well. Some new components are also included in ionic 2.
Let’s talk about the backend database with ionic 2. Nowadays requirements for offline application is becoming more popular. People want to have a quick and very much accurate application with or without internet connectivity. SQLite is a phenomenon, which can be useful in providing such feature.
SQLite is a software library that implements SQL operations using SQL database engine. SQLite is the widely deployed SQL database engine in the world.
SQLite is a local storage for the applications, SQLite is used for storing data similarly we store data in server side websites using MySQL. SQLite provides the similar functions and queries as we have in MySQL
SQLite provides faster access to the data in an application. One can fetch the data from the server and can save in SQLite database so that later on we can use it from SQLite only because of which application’s performance will good rather than normal application.
Note: SQLite stores its database on the local device so that you can’t find your data in another device also due to this the size of an application is also got increased.
Why we use SQLite
- In case you’re unfamiliar with local storage, it is key-value storage within your application.
- This makes it difficult to query for data when you have a lot of it.
- Not only is their query difficulties, but there are limitations. And local storage has a limitation of 10MB of data.
These reasons are what makes SQLite a better choice if we compare it against local storage.
Connection with SQLite
For using SQLite in our application we need to add the SQLite plugin given by Cordova in our ionic v2 application. It can be added via the following:
$ ionic plugin add cordova-sqlite-storage
$ npm install --save @ionic-native/sqlite
Here, in the platform.ready() function, we will create a new database and it will store in device, openDatabase() is a function for opening database, it has two cases if the Database does not exist then, it will create the DB and open it and if it already exists then, it will open that one. deleteDatabase() delete the database. add() function will add the firstname and lastname in userdata table. executeSql() Execute SQL on the opened database.
Note, you must open/create DB first, and ensure it resolved and successfully opened.