Web SQL删除查询不起作用

时间:2017-12-24 17:51:23

标签: javascript sql database cordova web-sql

我正在为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);
    });

}

但是代码不起作用并始终提供警报

  

[Object SQLError]

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

感谢

2 个答案:

答案 0 :(得分:0)

只要您想删除表中的所有数据,请尝试以下语句:

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);
         }
     )
});

答案 1 :(得分:0)

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

  

烤('删除&#39);

尚未定义的函数因此声明失败。 对不起我的不好并感谢您的帮助。