Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSQL, Phonegap. Return the ID of the last inserted row

How do I go about getting the last inserted (autoincremented) ID when writing to a WebSQL DB in Javascript for Phonegap?

I thought it may have been possible using the commented out snippet below.

Here is the structure of the table:

tx.executeSql('CREATE TABLE IF NOT EXISTS locations (id INTEGER PRIMARY KEY, lTitle TEXT, lDeleted INTEGER DEFAULT 0)');

And here is where I am writing the row to the table:

tx.executeSql(
            'INSERT INTO locations (lTitle) VALUES (?)',
            [$('#newLocation').val()],
            function(tx, results){
                //alert('Returned ID: ' + results.rows.item(0).id);
            },
            errorCB
        );
like image 453
Fraser Avatar asked Jul 22 '12 02:07

Fraser


1 Answers

I was close. This does the trick:

tx.executeSql(
            'INSERT INTO locations (lTitle) VALUES (?)',
            [$('#newLocation').val()],
            function(tx, results){
                alert('Returned ID: ' + results.insertId);
            },
            errorCB
        );
like image 118
Fraser Avatar answered Oct 06 '22 02:10

Fraser