我正在创建一个应用程序,在其中插入表中的数据。在将数据插入表后,我希望获得该行的ID,以便根据它进行进一步操作。 我尝试使用sqlite的last_insert_rowid()函数,但没有成功。 有人能告诉我获取最后插入的行ID的最佳方法吗? 感谢您的提前阅读。
我正在创建一个应用程序,在其中插入表中的数据。在将数据插入表后,我希望获得该行的ID,以便根据它进行进一步操作。 我尝试使用sqlite的last_insert_rowid()函数,但没有成功。 有人能告诉我获取最后插入的行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
)
WebSQL
中的 results.insertId
类似于 MySQL 中的 mysql_insert_id()
-
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
currentProfile
对象。我想在您的情况下,您可能会将其放在循环内部。在这种情况下,您可以将 tx.executeSql
包装在闭包中,以便它保持对 currentProfile
对象的范围。 - Ross你的表中是否有名为rowid、oid或_rowid_的列?如果有,那么使用last_insert_rowid()
时会出现问题。请参见这里。
解决方法: 如果你有一个自动递增的ID列,你可以使用以下SQL语句来获取最后一个条目。
SELECT * FROM yourTable ORDER BY yourIdColumn DESC LIMIT 1
这可能有点粗糙,但它可以工作。
"