javascript WebSQL、Phonegap。返回最后插入行的 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11597375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:46:31  来源:igfitidea点击:

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

javascriptsqlcordova

提问by Fraser

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

在使用 Javascript for Phonegap 写入 WebSQL DB 时,如何获取最后插入的(自动递增的)ID?

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

回答by Fraser

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