Skip to content

Instantly share code, notes, and snippets.

@coderdiaz
Last active February 14, 2016 02:52
Show Gist options
  • Save coderdiaz/42922bff98093f0cf7fd to your computer and use it in GitHub Desktop.
Save coderdiaz/42922bff98093f0cf7fd to your computer and use it in GitHub Desktop.
Manejo de Base de Datos SQLite con $cordovaSQLite en 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);
});
};
});
/* 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/
*/
@coderdiaz
Copy link
Author

Muchas gracias @jdnichollsc

@anficyon
Copy link

gracias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment