본문 바로가기

Mobile/Ionic2

Ionic SQLite

 출처: https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/


Use SQLite Instead of Local Storage In Ionic Framework

Not to worry, because there is a plugin for that!

Making use of the Cordova SQLite plugin by Chris Brody, you can use a SQLite data source for managing your data in Android and iOS.  Pair this with ngCordova and you can better compliment your Ionic Framework development with an AngularJS experience.

If you’re using Ionic 2, you should check out this tutorial instead.

Like with all my tutorials, let’s start by creating a fresh Ionic project:

Remember, if you’re not on a Mac computer, you cannot add and build for the iOS platform.

The next thing we want to do is add the SQLite plugin.  This can be done by running the following command:

Because this is an Ionic Framework article, we’re going to make use of ngCordova as it tends to make life pretty easy after including it.

Download the latest release and copy ng-cordova.min.js into your www/js directory.

With the library file included, we now need to include it in our project.  Open your index.html file and add the following highlighted line:

It is very important you add it above the cordova.js line otherwise it will not work.

One more thing must be added before we can start using ngCordova.  We need to inject it into our angular.module, found in app.js, like the following:

It’s time to start using this library.  For simplicity, we are going to do the following:

  1. Create a new table called people
  2. Insert two people into this new table
  3. Select all the people found in our table with my last name

Before we start coding, it is very important to note that database activity can only be done when the onDeviceReady() method has fired.  With this in mind, I’m going to open the database in the modules .run() method like so:

Take a look at the lines that I highlighted.  We want to have access to the database globally so I created a variable outside of any method or controller.  To use the ngCordova functions we need to include $cordovaSQLite in the .run() method.  Finally, you can see that I’ve created a new database called my.db and a fresh table called people.

This leaves us with a usable database ready for activity in our controllers.  Take the following for example:

I’ve gone ahead and created two very basic functions.  The insert function will add a first and last name record into the database while the selectfunction will find records by last name.  Basic, but I hope you get the idea.

Something to note about my controller though.  I am not doing these database calls from inside an onDeviceReady() function.  Had these functions been fired when the controller loads, they probably would have failed.  Since I am basing the database activity off button clicks, it is probably safe to assume the device and database is ready for use.

If you wish to delete any of your SQLite databases you can do so by including the following:

There are plenty of other ngCordova and baseline SQLite commands that you can make use of.  The plugin documentation is very thorough and worth a read.  If you want to use this SQLite plugin in Ionic 2, you should check out my tutorial on the subject.

A video version of this article can be seen below.