Use SQLite Instead of Local Storage In Ionic Framework【轉】

來源:互聯網
上載者:User

標籤:

Switching to object-based data storage can often be tough.  If you’re trying to start Phonegap or Ionic Framework development and are coming from native development, the whole local storage concept can be a tough one to grasp.  Or maybe you just prefer a structured query language (SQL) when working with your data.

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 withngCordova and you can better compliment your Ionic Framework development with an AngularJS experience.

 

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

ionic start IonicProject blankcd IonicProjectionic platform add androidionic platform add ios

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:

cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin.git

  

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:

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">        <title></title>        <link href="lib/ionic/css/ionic.css" rel="stylesheet">        <link href="css/style.css" rel="stylesheet">        <script src="lib/ionic/js/ionic.bundle.js"></script>        <script src="js/ng-cordova.min.js"></script>        <script src="cordova.js"></script>        <script src="js/app.js"></script>    </head>    <body ng-app="starter">

  

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:

angular.module(‘starter‘, [‘ionic‘, ‘ngCordova‘])

  

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:

var db = null; var example = angular.module(‘starter‘, [‘ionic‘, ‘ngCordova‘])    .run(function($ionicPlatform, $cordovaSQLite) {        $ionicPlatform.ready(function() {            if(window.cordova && window.cordova.plugins.Keyboard) {                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);            }            if(window.StatusBar) {                StatusBar.styleDefault();            }            db = $cordovaSQLite.openDB("my.db");            $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");        });    });

  

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:

 1 example.controller("ExampleController", function($scope, $cordovaSQLite) { 2   3     $scope.insert = function(firstname, lastname) { 4         var query = "INSERT INTO people (firstname, lastname) VALUES (?,?)"; 5         $cordovaSQLite.execute(db, query, [firstname, lastname]).then(function(res) { 6             console.log("INSERT ID -> " + res.insertId); 7         }, function (err) { 8             console.error(err); 9         });10     }11  12     $scope.select = function(lastname) {13         var query = "SELECT firstname, lastname FROM people WHERE lastname = ?";14         $cordovaSQLite.execute(db, query, [lastname]).then(function(res) {15             if(res.rows.length > 0) {16                 console.log("SELECTED -> " + res.rows.item(0).firstname + " " + res.rows.item(0).lastname);17             } else {18                 console.log("No results found");19             }20         }, function (err) {21             console.error(err);22         });23     }24  25 });

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 select function 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:

1 $cordovaSQLite.deleteDB("my.db");

 

註:原文地址:https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/#

 

Use SQLite Instead of Local Storage In Ionic Framework【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.