【问题标题】:Web SQL Delete Query not WorkingWeb SQL 删除查询不起作用
【发布时间】:2017-12-24 17:51:22
【问题描述】:

我正在为 cordova 应用程序编写代码,以便在函数调用时从 Web SQL DB 表中删除所有数据。

这里是代码

function removeitem(){
db.transaction(function (tx) {
tx.executeSql("DELETE FROM hist", [], function (tx, result) {
toast('Deleted');
}, function (error) {
        alert(error.code);
}); 

}, function (error) {
        alert(error);
    });

}

但代码不起作用,总是提示警报

[对象 SQLError]

用于创建表的其他功能,更新记录工作正常,但删除查询正在产生问题。请帮助大家找出问题所在。

谢谢

【问题讨论】:

  • 尝试在实际数据库上使用“DELETE FROM hist”进行检查。
  • @AslanShemilov 当我这样做时它确实有效。
  • 你是否已经尝试过这个语句:DELETE FROM hist WHERE .....你必须有一个 where 条件。
  • 除非你想清空整个表,否则你必须有 WHERE。
  • 你至少可以试试“DELETE FROM hist WHERE 1”

标签: javascript sql database cordova web-sql


【解决方案1】:

只要你想从表中删除所有数据,试试下面的语句:

TRUNCATE TABLE hist;

tx.executeSql("TRUNCATE TABLE hist",[], 
    function(tx,results){console.log("Successfully Emptied")},
    function(tx,error){console.log("Could not Empty")}
);

tx.executeSql("DELETE FROM hist",[], 
    function(tx,results){console.log("Successfully Emptied");},
    function(tx,error){console.log("Could not Empty");}
);

其中一个必须有效。

完整代码:

var db.transaction(function (tx) {
     tx.executeSql("DELETE FROM hist",[], 
         function(tx,results){
             console.error("Successfully Emptied");
         },
         function(tx,error){
             console.error("Error: " + error.message);
         }
     )
});

【讨论】:

    【解决方案2】:

    我刚刚注意到,这是一个愚蠢的错误。我在打电话

    toast('已删除');

    尚未定义的函数,因此该语句失败。 对不起我的不好,谢谢你的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2017-02-25
      • 2015-10-01
      • 2015-03-25
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多