简体   繁体   English

从网络数据库删除记录的正确JS语法是什么?

[英]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: 但是,有几个相关的问题:

  1. The condition used is wrong, SQL uses = for equality. 使用的条件是错误的,SQL使用=表示相等。

  2. 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.

相关问题 通过前端的 fetch 请求访问 Active Record 嵌套对象的正确方法/语法是什么? - What's the proper way/syntax to access Active Record nested objects through the frontend's fetch request? 关于从数据库中删除记录的确认 - confirmation about deleting a record from database 在Node.js中删除/释放Twilio电话号码的语法是什么? - What is the syntax for deleting/releasing Twilio phone numbers in Node.js? 什么是 settimeout 正确的语法? - What is settimeout proper syntax? 在JavaScript中,通过条件语句更改css的正确语法是什么? - What's the proper syntax, in JavaScript, for changing css via conditional statements? 从数据库中删除记录后重新加载页面(表)的问题 - Problem in reloading page(table) after deleting record from database 从数据库中删除记录的按钮不起作用。 rest ajax - The button for deleting a record from the database does not work. rest ajax 使用 ajax 请求从数据库实体框架中删除记录 - Deleting record from the database entityframework using ajax request HAML中的Ruby in JS:语法是什么? - Ruby in JS in HAML: what's the syntax? 从ExtJS 4中的表单中卸载/取消记录的正确方法是什么? - What is the proper way to unload/unbind a record from a form in ExtJS 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM