Although the Ionic Storage package is great to store almost any kind of data, many of the readers wrote they are using MySQL on the backend side and need some more equivalent storage inside their Ionic app.
Although the Ionic Storage package is great to store almost any kind of data, many of the readers wrote they are using MySQL on the backend side and need some more equivalent storage inside their Ionic app.
In this post we will see how to easily import SQL data into our Ionic app, and how to work with queries to retrieve or modify our stored data!
Note: If you have some sort of MySQL database you should still work on a REST API to deliver the data and don’t work directly on the database.
We start with a blank Ionic app and install the Cordova SQLite plugin to access the database. Additional we add the SQLite Porter plugin as well as the according Ionic Native NPM packages so we can populated our SQL database from e.g. a given SQL dump. Go ahead and run:
We also create a provider for all of our database interaction right here.
As said before we want to fill our database with some initial values. You can do this of course with the standard SQLite wrapper but you would either have to prepare each statement accordingly or split up your SQL dump into the right format, which is sometimes not a good idea.
The SQLite Porter plugin would also allow to import JSON data, but here we stick to SQL.
In our case it’s enough to have a SQL dump like…