Search code examples
sqliteionic-frameworkionic3cordova-pluginsionic-native

Ionic 3 native SQLite plugin is not working as expected


I use Ionic 3 native SQLite plugin. But below code is not working as expected. i.e. I cannot see console.log() data for the inserted data. It seems I'm doing wrong here. Can you tell me the right way?

Note: No errors. Just not working.

  storeApiKeyInSqlite(key: string, name: string) {
    this.sqlite.create({
      name: 'MyInvoices.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql('CREATE TABLE IF NOT EXISTS Apikeys(Id INT PRIMARY KEY NOT NULL, ApiKey NVARCHAR(100) NOT NULL, ApiName NVARCHAR(100) NULL)', [])
        .then(() => {
          db.executeSql('INSERT INTO Apikeys VALUES(NULL,?,?)', [key, name])
            .then(() => {
              db.executeSql('SELECT * FROM Apikeys', [])
                .then(res => {
                  if (res.rows.length > 0) {
                    console.log(res.rows.item(0).Id);
                    console.log(res.rows.item(0).ApiKey);
                    console.log(res.rows.item(0).ApiName);
                  }
                })
                .catch(e => {
                  console.log(e);
                });
            }).catch(e => {
              e => console.log(e)
            });
        }).catch(e => console.log(e));


    }).catch(e => console.log(e));
  }

Solution

  • Op's feedback:

    This is the working solution for me:

     storeApiKeyInSqlite(key: string, name: string) {
        this.sqlite.create({
          name: 'MyInvoices.db',
          location: 'default'
        }).then((db: SQLiteObject) => {
          db.executeSql('CREATE TABLE IF NOT EXISTS Apikeys(rowid INTEGER PRIMARY KEY,ApiKey NVARCHAR(100) NOT NULL, ApiName NVARCHAR(100) NULL)', [])
            .then(() => {
              db.executeSql('INSERT INTO Apikeys VALUES(NULL,?,?)', [key, name])
                .then(() => {
                  db.executeSql('SELECT * FROM Apikeys', [])
                    .then(res => {
                      if (res.rows.length > 0) {
                        console.log(res.rows.item(0).rowid);
                        console.log(res.rows.item(0).ApiKey);
                        console.log(res.rows.item(0).ApiName);
                      }
                    })
                    .catch(e => {
                      console.log(e);
                    });
                }).catch(e => {
                  console.log(e)
                });
            }).catch(e => console.log(e));
        }).catch(e => console.log(e));
      }
    

    Original answer:

    You cant insert NULL for a PRIMARY KEY which you explicitly set as NOT NULL.

    Make a query to:

    db.executeSql('INSERT INTO Apikeys(ApiKey,ApiName) VALUES(?,?)', [key, name])
                .then(() => {
                  db.executeSql('SELECT * FROM Apikeys', [])
                    .then(res => {
                      if (res.rows.length > 0) {
                        console.log(res.rows.item(0).Id);
                        console.log(res.rows.item(0).ApiKey);
                        console.log(res.rows.item(0).ApiName);
                      }
                    })
                    .catch(e => {
                      console.log(e);
                    });
                }).catch(e => {
                  e => console.log(e)
                });