views:

1512

answers:

2

Webkit (on iPhone and Safari, at least) supports HTML5's database storage via SQLite.

I'm trying to figure out how best to get the insert ID (there's an autoincrement field 'id' in the database) for the following transaction.

db.transaction(function(tx) {
  tx.executeSql("INSERT INTO teams (name) VALUES (?)", [$('#team_name').val()]);
});
A: 

Something like this ought to work:

SELECT last_insert_rowid();
Paul Lefebvre
+3  A: 

The code sample below is from Apple's documentation, I know it works on iPhone and Safari, and probably WebKit. You can get the insert id from the resultSet response object by using resultSet.insertId Also, you can get the number of affected rows, for an update query for example by using the rowsAffected property of the resultSet object.

db.transaction(
function (transaction) {
    transaction.executeSql('INSERT into tbl_a (name) VALUES ( ? );',
        [ document.getElementById('nameElt').innerHTML ],
        function (transaction, resultSet) {
            if (!resultSet.rowsAffected) {
                // Previous insert failed. Bail.
                alert('No rows affected!');
                return false;
            }
            alert('insert ID was '+resultSet.insertId);
            transaction.executeSql('INSERT into tbl_b (name_id, color) VALUES (?, ?);',
                [ resultSet.insertId,
                  document.getElementById('colorElt').innerHTML ],
                nullDataHandler, errorHandler);
        }, errorHandler);
}, transactionErrorCallback, proveIt);

Apple's HTML5 Database Documentation

Heat Miser
Thanks, that's what I needed! :-D
ceejayoz
Glad I could help!
Heat Miser