Skip to content

Instantly share code, notes, and snippets.

Created September 22, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/9341a87f7194ef3157bd929ce075533d to your computer and use it in GitHub Desktop.
Save anonymous/9341a87f7194ef3157bd929ce075533d to your computer and use it in GitHub Desktop.
//aqui no controller
chatsFactory.select().then(function(res){
if (res!=null) {
$scope.urldireciona = res.url;
// $scope.urldireciona = res["licenca"];
console.log($scope.urldireciona);
} else {
console.log('No records found');
}
})
//aqui mexemos com o banco de dado sqlite
$conjunto.factory('chatsFactory', function($cordovaSQLite,$ionicPopup, $state,$http) {
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "DBUrl.db", location:"default" }); //device
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chats (id integer primary key AUTOINCREMENT, url text, licenca text)");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS Usuario (id INTEGER PRIMARY KEY AUTOINCREMENT,matricula TEXT not null, cpf TEXT not null );");
}else{
db = window.openDatabase("DBUrl.db", '1', 'DBUrl', 1024 * 1024 * 100); // browser
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS enderecos (id integer primary key AUTOINCREMENT, url text, licenca text)");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS Usuario (id INTEGER PRIMARY KEY AUTOINCREMENT,matricula TEXT not null, cpf TEXT not null );");
}
return {
//aqui inserimos na tabela enderecos chatsFactory.insert
insert : function(url, licenca) {
var query = "INSERT INTO enderecos (url, licenca) VALUES (?, ?)";
var values = [url,licenca];
$cordovaSQLite.execute(db, query, values).then(
function(res) {
$state.go('app.loginmat');
},
function(err) {
alert("Erro ao Inserir no Sqlite!");
}
);
},
//selecionamos a tabela enderecos
select : function() {
var query = "SELECT distinct(url), licenca FROM enderecos ";
return $cordovaSQLite.execute(db, query).then(
function(res) {
if (res.rows.length > 0) {
var first = res.rows.item(0);
// console.log('Url: ' + first.url);
return first;
// $state.go('app.loginmat');
} else {
console.log('No records found');
}
}
);
},
//inserimos na tabela usuario
insertUsuario : function(matricula, cpf) {
var query = "INSERT INTO Usuario (matricula, cpf) VALUES (?, ?)";
var values = [matricula,cpf];
$cordovaSQLite.execute(db, query, values).then(
function(res) {
alert("deu certo");
},
function(err) {
alert("deu erro");
}
);
},
//selecionamos da tabela usuario
selectUsuario : function() {
var query = "SELECT * FROM Usuario ";
$cordovaSQLite.execute(db, query).then(
function(res) {
if (res.rows.length > 0) {
var first = res.rows.item(0);
console.log(res.rows.length + ' records, fist: ' +res.matricula+' '+res.cpf);
} else {
console.log('No records found');
}
}
);
},
//deletamos das duas tabelas
deletabd : function() {
var query = "Delete from Usuario";
$cordovaSQLite.execute(db, query).then(
function(res) {
console.log("apagou tudo");
}
);
var queryurl = "Delete from enderecos";
$cordovaSQLite.execute(db, queryurl).then(
function(res) {
console.log("apagou tudo");
}
);
}
}
//vou fazer fora
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment