Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting an insert ID from Webkit's HTML5 database storage

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()]);
});
like image 690
ceejayoz Avatar asked Mar 21 '09 20:03

ceejayoz


1 Answers

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

like image 184
Heat Miser Avatar answered Oct 05 '22 21:10

Heat Miser