[英]What's proper JS syntax for deleting a record from a web database?
I use the following to display, with innerHTML, the Javascript-based webSQL data (containing 4 fields) on the HTML page: 我使用以下内容通过innerHTML在HTML页面上显示基于Javascript的webSQL数据(包含4个字段):
var rowid = results.rows.item(i).id;
document.getElementById("output2").innerHTML += "<table><tr><td><index type='button' class='buttonDel' onclick='delRecord(\"" + rowid + "\")' value='Delete'>" + results.rows.item(i).id + "</td><td>" + results.rows.item(i).data1 + "</td><td>" + results.rows.item(i).data2 + "</td><td>" + results.rows.item(i).data3 + "</td></tr></table>";
A red Delete button appears in the ID column of the HTML table with the correct id put into var rowid (confirmed with an alert). 在HTML表的ID列中会出现一个红色的Delete按钮,并将正确的ID放入var rowid中(已通过警报确认)。 The button calls the following functions to delete the record.
该按钮调用以下功能删除记录。 Of course, that doesn't happen.
当然,那不会发生。
// Delete a row in the DB
function delRecord(rowid) {
var db = window.openDatabase("Database", "1.0", "DEMO", 2000000);
db.transaction(delDB, errorCB, successCB);
}
function delDB(tx) {
tx.executeSql("DELETE from DEMO WHERE id == rowid");
}
What is the correct syntax to delete the record? 删除记录的正确语法是什么?
Here's how the buttons are being formatted: 按钮的格式化方法如下:
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("Returned rows = " + results.rows.length);
for (var i = 0; i < len; i++) {
var rowid = results.rows.item(i).id;
document.getElementById("output2").innerHTML +=
"<table><tr><td><index type='button' class='buttonDel' onclick='delRecord(\"" +
rowid + "\")' value='Delete'>" +
results.rows.item(i).id +
"</td><td>" + results.rows.item(i).data1 +
"</td><td>" + results.rows.item(i).data2 +
"</td><td>" + results.rows.item(i).data3 + "</td></tr></table>";
}
The correct syntax is DELETE FROM tableName [WHERE expr]
(The underlying implementations all use SQLite, AFAIK.) 正确的语法是
DELETE FROM tableName [WHERE expr]
(基础实现全部使用SQLite,AFAIK。)
However, there are several related problems: 但是,有几个相关的问题:
The condition used is wrong, SQL uses =
for equality. 使用的条件是错误的,SQL使用
=
表示相等。
rowid
, as string content, is wrong. rowid
作为字符串内容是错误的。 That will never be the ID because no "magic replacement" is done. 那将永远不会是ID,因为没有完成“魔术替换”。 Instead, use a parameterized query and specify the bound value.
而是使用参数化查询并指定绑定值。
Consider: 考虑:
function deleteRecord(rowid) {
var db = getCurrentDatabase();
db.transaction(
function (tx) {
tx.executeSql("DELETE FROM demo WHERE id = ?", [rowid]);
});
}
I also used a closure to make rowid
readily accessible. 我还使用了闭包使
rowid
易于访问。 Refer to some tutorials for more general and examples. 有关更多常规和示例,请参考一些教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.