i’ve been trying to fetch some data from the SQLite database through the SQLite plugin ($ ionic cordova plugin add cordova-sqlite-storage /$ npm install --save @ionic-native/sqlite).
In my home.component.ts I did:
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
...
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers:[recuperadorService, SQLite, databaseService]
})
...
constructor(public http: Http, public navCtrl: NavController, public navParams: NavParams, private sqlite: SQLite, public bd: databaseService) {}
...
testselectsql(){
this.message = this.bd.select();
}
and in my service I did:
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Injectable()
export class databaseService{
db: Promise<any>;
retorno: any;
constructor( public sqlite: SQLite){
}
select(){
this.open().then((teste) => {teste.executeSql("select * from danceMoves", {}).then((data) => {
for(var i =0; i< data.rows.length;i++){
this.retorno += data.rows.item(i).name;
}
} )});
return this.retorno;
}
open(){
return this.sqlite.create({name: 'data.db', location: 'default'});
}
When i click on a button to call the “testselectsql()” function, nothing happens at first, butf i click on it the second time, I can get the result expected from the database.
The weird thing is, that when I put the logic of the service inside my home.component.ts, on the first click I can get the result expected.
I find this attitude rather frustrating. I am trying to help you. Did you implement all that advice? If so, did you discover that maybe it fixed your problem?
on the first method call, everything inside “(teste) => {teste.executeSql(“select * from danceMoves”, {}).then(” it’s not returned. When I call this method again, it runs normally.