Search code examples
sqlitecordovaandroid-sqliterowid

Cordova / SQLite: rowid undefined


With SQLite and Cordova on an Android device.

I have a database containing a table with some fields.

None of the fields is a primary key so the records have a default rowid generated by SQLite.

Here is a screenshot of the table in Chrome debugger:table in Chrome debugger

I'm trying to access that rowid (I spare you all the database code and focus on the query):

tx.executeSql('SELECT * FROM ServicesRondes', [], function(tx, res) {
    console.log(res.rows.length); // shows 5
    if (res.rows.length > 0) {
        for(var i = 0; i < res.rows.length; i++) {
            console.log(res.rows.item(i).time); // shows the time record
            console.log(res.rows.item(i).rowid); // shows undefined
        }
    }               
}

So the problem is that the rowid is undefined in the logs.

I've found only one link with a guy answering his own question: https://forum.qt.io/topic/53217/solved-how-to-access-sqlite-rowid

As you can see, he made a query with rowid to solve his problem.

But it's not really what I want (I want to select all) and furthermore, I don't get why it works this way and not the other way.

Of course I could create my own autoincremented primary key field but I'd like to avoid it and this is not my question.


Solution

  • SELECT * returns all columns that you have declared in the table definition.

    If you want the rowid column to show up with *, you have to include it there:

    CREATE TABLE ServicesRondes (
        rowid     INTEGER PRIMARY KEY,
        userUuid  UUID,
        secteurId UUID,
        time      TIMESTAMP,
        [...]
    );