Skip to content

Instantly share code, notes, and snippets.

@nufaylr
Last active October 26, 2015 09:52
Show Gist options
  • Select an option

  • Save nufaylr/59f77ce8b474f817a174 to your computer and use it in GitHub Desktop.

Select an option

Save nufaylr/59f77ce8b474f817a174 to your computer and use it in GitHub Desktop.
SQLite with ngCordov Code Examples
angular.module("starter").controller('userController', [ '$rootScope', '$cordovaSQLite'
function( $rootScope, $cordovaSQLite ) {
// Opening the database
$rootScope.db = $cordovaSQLite.openDB({ name: "databaseName.db" });
// Create tabel if not existing
$cordovaSQLite.execute($rootScope.db,
"CREATE TABLE IF NOT EXISTS user_tabel (id integer primary key, user_name text, email text)",
[ "john", "john@who.com" ]).then(function(res) {
console.log(res.insertId);
}, function (err) {
console.error('e', err);
}
);
// Select items from the table
$cordovaSQLite.execute($rootScope.db,
"SELECT id, user_name, email FROM user_tabel",[ ]).then(function(result) {
if(result.rows.length > 0) {
console.log('', result)
}
}, function (err) {
console.error('err : ', err);
});
// Drop Tabel if existing
$cordovaSQLite.execute($rootScope.db,
"DROP TABLE IF EXISTS user_tabel", [ ]).then(function(res) {
console.log('DROPED TABLE user_tabel');
}, function (err) {
console.error('DROP ERR user_tabel: ', err);
});
// Update Tabel
$cordovaSQLite.execute($rootScope.db,
"UPDATE user_tabel SET email = 'john20@who.com' WHERE name = ?", [ ]).then(function(res) {
console.log('DROPED TABLE user_tabel');
}, function (err) {
console.error('DROP ERR user_tabel: ', err);
});
// Join User Table and Information Tabel from user_id
$cordovaSQLite.execute($rootScope.db,
"SELECT DISTINCT u.name FROM user_tabel u INNER JOIN info_tabel i ON i.id = u.id WHERE i.id = ?", [20]).then(function(result) {
if(result.rows.length > 0) {
console.log(result);
}
}, function (err) {
console.error('JOIN ERR : ', err);
});
}]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment