我正在创建一个应用程序,在该应用程序中我在表中插入数据。在表中插入数据后,我想获取该行的 id,以便我可以根据它进行进一步的操作。我尝试使用sqlite 的last_insert_rowid()函数,但没有运气。任何人都可以告诉哪个是获取最后插入的行ID的最佳方法。任何想法都值得赞赏。提前致谢。
问问题
11193 次
2 回答
8
您可以像这样在表上获取最后插入的 id -
tx.executeSql("INSERT INTO profile('name','label','list_order','category') values(?,?,?,?)",
[currentProfile.name, currentProfile.label, currentProfile.list_order, currentProfile.category],
function(tx, results){
var lastInsertId = results.insertId; // this is the id of the insert just performed
},
failCB
)
results.insertId
WebSQL 中的与mysql_insert_id()
MySQL中的类似 -
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
于 2013-10-16T16:14:17.227 回答
1
Do you have a column in your table named rowid, oid, or _rowid_? If you do, that will cause an issue with last_insert_rowid()
working as intended. See here.
A workaround: If you have an ID column that is auto-incremented, you could use the following SQL to get the last entry.
SELECT * FROM yourTable ORDER BY yourIdColumn DESC LIMIT 1
It's hacky, but it would work.
于 2013-10-16T11:28:37.647 回答