SQLite Problem

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

This looks tailor-made for ionic-storage. I see no need for direct interaction with SQLite here.

Thanks for the response…

Even though the errors are occurring… Is there another way to use SQLite? Maybe I’m using it wrong…

As @rapropos said, use Ionic Storage, not SQLite, to solve your problem.

A yes, I understand.

Any way you need to do SQLite to save other information …

Does anyone know how to help me?

Reading through your post again you are using Ionic Native, not ngCordova.

Can you write to the database table?
Is there actually content in the database table?
What code exactly is not working?

I believe you do not save data…
Even without records, I should not fall into the error, am I right?

The error is always returned when trying to fetch the records from the
table …

To use ngCordova, what should I do?

Thanks

“you do not” - translation mistake? “it does not”?

If there are 3 steps, it doesn’t make sense to wonder about #3 if #2 is still broken. For example it can’t output anything, if there is nothing in the database.

You shouldn’t and you don’t. ngCordova was for Ionic 1, you are using a newer version looking at the code you posted.

hey, if you want to use SQLite see examples in my project https://github.com/luckychel/VLTPayback/blob/master/src/services/settings-service.ts

1 Like

Yes, translation error :S

I understand.

When I try to save, it does not return me error (promise error), but I can
not tell if it saved because the query is broken … I’m thinking of
deleting everything and starting over, rs.

Do you have a working example where I can base it?

Thanks again!!

Don’t know if you are still facing issues with sqlite. Maybe following can help someone.

I would recommend this - https://gist.github.com/aggarwalankush/0b700328e797e22a1d9994cb35afdf09

See usage in this app - https://github.com/aggarwalankush/ionic2-mosum/blob/master/src/pages/providers/database.service.ts

Thank you all!

The problem was solved using the ankushagg93 method!!