【问题标题】:Why is my delete where statement not working? [closed]为什么我的 delete where 语句不起作用? [关闭]
【发布时间】:2015-01-27 17:12:17
【问题描述】:

我正在尝试从表 USERS 中删除用户。不幸的是,我一直被发送到 onSqlError。我不知道为什么。当我提醒请求时,它会为我提供 getTheId 变量的正确名称。对sql不太熟悉,所以也许我写错了。任何指针表示赞赏。

// DELETE RECORD
function deleteRecord(getTheId){
    deleteUser.onclick = (function () {//deleteUser is an element generated for each user when a button is clicked
        var sqlStr2 = 'DELETE FROM USERS WHERE username = '+getTheId+'';
        alert("SQL: " + sqlStr2); //This gives me the statement above with the correct name of the user clicked.
        if (db){
            console.log("db is there");//this logs
            db.transaction(function(tx) {  
                   tx.executeSql(sqlStr2);
            }, onSqlError, onSqlSuccess); //THEN I GET SENT TO ERROR
        }
    });
}

【问题讨论】:

  • 用户名需要用引号引起来。
  • 请注意,Web SQL standard 已被废弃 4 年以上。虽然,实现确实允许使用占位符 -- WHERE username = ? -- 因此可以提供参数而不会有SQL Injection 的风险 -- tx.executeSQL(sqlStr2, [getTheId]);
  • 谢谢巴马尔。由于您没有留下答案,因此我已将您的评论标记为有用。 Majid L 的解决方案有效。
  • 谢谢乔纳森。我会尝试按照您建议的格式实施解决方案。

标签: javascript sql sqlite


【解决方案1】:

试试这个:

var sqlStr2 = "DELETE FROM USERS WHERE username = '"+getTheId+"'";

【讨论】:

    【解决方案2】:

    尝试添加一个';'到你声明的结尾,虽然没有错误信息很难说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 2011-07-16
      相关资源
      最近更新 更多