[SOLVED] Can't retrieve id using sqlite database

Hi,
I’m not able to get the id of a row using SQLite.
When I try to retrieve the id I get “undefined”. However if I put an ID with a given and valid value of a row on my delete statement it works.
Here is my code :

in Test.html

<ion-list>
    <ion-item-sliding *ngFor="let u of user">
         <ion-item>
             {{u.firstname}} {{u.lastname}} {{u.id}}
         </ion-item>
         <ion-item-options>
             <button ion-button icon-only (click)="delete(u)" danger>
                 <ion-icon name="trash"></ion-icon>
             </button>
         </ion-item-options>
      </ion-item-sliding>
  </ion-list>

In Test.js

public delete(u) {
      console.log(u);
      console.log(u.firstname);
      console.log(u.id); //Return undefined
      var query = "DELETE FROM user WHERE `id` = ?";
      var curr_id = u.id;
      this.database.create({name: "data.db", location: "default"}).then((db: SQLiteObject) => {
        db.executeSql(query, [u.id]).then((data) => {
              console.log("DELETED: " + JSON.stringify(data));
          }, (error) => {
              console.log("ERROR: " + JSON.stringify(error.err));
          });
        });
    }

How I create my db :

let db = new SQLite();
            db.create({
                name: "data.db",
                location: "default"
            }).then((db: SQLiteObject) => {
                db.executeSql("CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", {}).then((data) => {
                    console.log("TABLE CREATED: ", data);
                }, (error) => {
                    console.error("Unable to execute sql", error);
                })
            }, (error) => {
                console.error("Unable to open database", error);
            });

Thanks for helping me !

Ja

I found out that I didnt add my id to my array user that contains the results of a select * !

If you’re always accessing these rows by id, I would switch to using ionic-storage instead of direct SQLite. Works in browsers as well, much simpler.

1 Like

But I thought ionic-storage work as a key-value storage ? I choose direct SQLite because I’ll need several table with junctions, I don’t know if it was the right thing to do but I just start using ionic