最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSQL, Phonegap. Return the ID of the last inserted row - Stack Overflow

programmeradmin0浏览0评论

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
        );

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
        );
Share Improve this question asked Jul 22, 2012 at 2:26 FraserFraser 14.2k22 gold badges76 silver badges119 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 32

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
        );
发布评论

评论列表(0)

  1. 暂无评论