I’m creating an application with ionic2 using the ngCordova sqlite plugin.
I am not able to return the values in the query, and I can not tell if I am able to save them…
My code:
Database.ts
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Injectable()
export class Database {
public storage;
constructor( public sqLite: SQLite = new SQLite()) {
this.sqLite.create({name: "appname.db", location: "default"})
.then((db: SQLiteObject) => {
this.storage = db;
this.tryInit();
}, (error) => {
console.log("ERROR: ", error);
});
}
tryInit() {
this.query('CREATE TABLE IF NOT EXISTS configuracoes (item VARCHAR(100) PRIMARY KEY, conteudo VARCHAR(255) )')
.catch(err => {
console.error('Unable to create initial storage tables', err.tx, err.err);
});
}
query(query: string, params: any[] = []): Promise<any> {
return new Promise((resolve, reject) => {
try {
this.storage.transaction((tx: any) => {
tx.executeSql(query, params,
(tx: any, res: any) => resolve({ tx: tx, res: res }),
(tx: any, err: any) => reject({ tx: tx, err: err }));
},
(err: any) => reject({ err: err }));
} catch (err) {
reject({ err: err });
}
});
}
}
providers/configuracoes.ts
constructor(public database: Database = new Database()) {
}
load(){
this.select('api_ip', (value) => {console.log(value,'select'), this.api.ip = value} );
`Preformatted text`}
select(item, cb){
console.log('SELECT');
this.database.query("SELECT conteudo FROM configuracoes WHERE item=?", [item.trim()]).then( (data)=>{
console.log('select');
console.log(data);
console.log(data.rows);
console.log(data.rows.item);
console.log(data.rows.item(0));
console.log(data.rows.item(0).conteudo);
cb(data.rows.item(1).conteudo);
}, (err) => {
console.log('err')
console.log(JSON.stringify(err));
});
}
In console logs:
[00:31:25] console.warn: Ionic Native: deviceready did not fire within 5000ms. This can happen when plugins are in an
inconsistent state. Try removing plugins from plugins/ and reinstalling them.
[00:31:25] console.log: Ionic Native: deviceready event fired after 8358 ms
[00:31:27] console.log: OPEN database: appname.db
[00:31:27] console.log: SELECT
[00:31:27] console.log: SELECT
[00:31:27] console.log: SELECT
[00:31:27] console.log: SELECT
[00:31:28] console.log: ionViewDidLoad ConfiguracoesPage
[00:31:28] console.log: err
[00:31:28] console.log: {“err”:{}}
[00:31:28] console.log: err
[00:31:28] console.log: {“err”:{}}
[00:31:28] console.log: err
[00:31:28] console.log: {“err”:{}}
[00:31:28] console.log: err
[00:31:28] console.log: {“err”:{}}
[00:31:28] console.log: OPEN database: appname.db - OK
Please help me…
Thanks