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 with ngCordova 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:
HTML<!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 @R_404_6839@="lib/ionic/CSS/ionic.CSS" rel="stylesheet"> <link @R_404_6839@="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:
Create a new table called peopleInsert two people into this new tableSelect 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','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({ name: "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:
example.controller("ExampleController",function($scope,$cordovasqlite) { $scope.insert = function(firstname,lastname) { var query = "INSERT INTO people (firstname,lastname) VALUES (?,?)"; $cordovasqlite.execute(db,query,[firstname,lastname]).then(function(res) { console.log("INSERT ID -> " + res.insertID); },function (err) { console.error(err); }); } $scope.select = function(lastname) { var query = "SELECT firstname,lastname FROM people WHERE lastname = ?"; $cordovasqlite.execute(db,[lastname]).then(function(res) { if(res.rows.length > 0) { console.log("SELECTED -> " + res.rows.item(0).firstname + " " + res.rows.item(0).lastname); } else { console.log("No results found"); } },function (err) { console.error(err); }); }});
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:
$cordovasqlite.deleteDB("my.db");
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.
总结以上是内存溢出为你收集整理的Use SQLite Instead of Local Storage In Ionic Framework全部内容,希望文章能够帮你解决Use SQLite Instead of Local Storage In Ionic Framework所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)