Last active
February 14, 2016 02:52
-
-
Save coderdiaz/42922bff98093f0cf7fd to your computer and use it in GitHub Desktop.
Manejo de Base de Datos SQLite con $cordovaSQLite en Ionic Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}; | |
}); | |
/* Fuente de información: | |
* https://www.youtube.com/watch?v=lxrsT4n86YI | |
* Ejemplo de como utilizar $cordovaSQLite Plugin para manejo | |
* de base de datos locales en Ionic Framework por Nic Raboy. | |
* Blog: https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mira mi ejemplo utilizando Service Pattern 😃 https://gist.github.com/jdnichollsc/9ac79aaa3407e92677ba