Skip to content

Instantly share code, notes, and snippets.

@raphaelluiz128
Last active April 6, 2018 17:44
Show Gist options
  • Save raphaelluiz128/6158048b2e751b34b2aadc94ff1ff40b to your computer and use it in GitHub Desktop.
Save raphaelluiz128/6158048b2e751b34b2aadc94ff1ff40b to your computer and use it in GitHub Desktop.
database ts json externo
import { HttpClient} from '@angular/common/http';
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { Form } from 'ionic-angular';
/*
Generated class for the DatabaseProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class DatabaseProvider {
private db: SQLiteObject;
public isOpen: boolean;
//public arrayMedicoes = [];
//public arrayMedicoes:Array<{[]}>;
public arrayMedicoes:Array<{}>;
//public dbCriada:boolean;
constructor(public http: Http,
public storage: SQLite) {
if (!this.isOpen){
this.storage = new SQLite();
this.storage.create({name:"data.db",location:"default"}).then((db:SQLiteObject) => {
this.db = db;
db.executeSql("CREATE TABLE IF NOT EXISTS MEDICOES(ID INTEGER PRIMARY KEY,DATA TEXT,HORA TEXT,NIVEL TEXT)",[]);
this.isOpen = true;
//console.log(this.isOpen);
//this.GetAllMedicoes();
//this.dbCriada=true;
}).catch((error)=>{
console.log(error);
})
}
}
CreateMedicao(data:string,hora:string,nivel:string){
return new Promise((resolve, reject) => {
//this.db.executeSql('delete from medicoes',[]);
this.db.executeSql('insert into medicoes (data,hora,nivel) values (?,?,?)', [data, hora, nivel]).then((data) => {
resolve(data); }, (error) => {
reject(error);
});
});
}
DeletarDados(){
//this.db.executeSql('delete from medicoes',[]);
this.db.executeSql('delete from medicoes where data in (select data from medicoes group by data having Count(data)>1) and hora in (select hora from medicoes group by hora having Count(hora)>1)',[]);
};
GetAllMedicoes(){
return new Promise((resolve,reject) => {
this.db.executeSql('select * from medicoes order by id desc',[]).then((data) =>{
if (data.rows.length >0) {
// alert(data.rows.item(0).HORA);
alert (data.rows.length);
/*
for (var i=0; i< data.rows.length;i++){
this.arrayMedicoes.push({
this.arrayMedicoes={
ID:data.rows.item(i).ID,
DATA:data.rows.item(i).DATA,
HORA:data.rows.item(i).HORA,
NIVEL:data.rows.item(i).NIVEL
};
}
*/
this.arrayMedicoes = data.rows.map( (row, i) => {
return {
ID: row.item(i).ID,
DATA: row.item(i).DATA,
HORA: row.item(i).HORA,
NIVEL: data.rows.item(i).NIVEL
}
});
}
//if (data.rows.length <= 0) {
//this.CriarTabelaVazia();
// }
resolve(this.arrayMedicoes);
//const pegarId = arrayMedicoes.map(({id}) => id);
// alert(pegarId);
// alert(data.rows.item(2).data);
}, (error) => {
reject(error);
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment