How to use $cordovaSQLite in Ionic Framework

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(false); } if(window.StatusBar){ StatusBar.styleDefault(); } db = $cordovaSQLite.openDB({ name: 'app.db' }); $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)"); }); }); 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(result) { console.log("INSERT ID -> " + result.insertId); }, function(error) { console.error(error); }); }; $scope.select = function(lastname) { var query = "SELECT firstname, lastname FROM people WHERE lastname = ?"; $cordovaSQLite.execute(db,query,[lastname]).then(function(result) { if(result.rows.length > 0) { console.log("SELECTED -> " + result.rows.item(0).firstname + " " + result.rows.item(0).lastname); } else { console.log("NO ROWS EXIST"); } }, function(error) { console.error(error); }); }; });

4 Responses

this script work?
please add update command example
because i am facing problem in update command..console.log say updated but actually value not updated.....
this code is not working
give error "Uncaught TypeError: $cordovaSQLite.openDatabase is not a function"

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.