选择查询的顺序不对

时间:2016-07-29 20:15:33

标签: sqlite cordova

我试图在数据库中查询数据。但是,当我查询我的数据时,我只设法在我的上次警报之后获取数据,即alert("Total...")例如,我运行下面的代码,“结果行:”的警报仅出现在出现最后警报。我可以知道为什么吗?

function (result) {

           window.location.href = "#/app/CustHomePage";

           var totalBalance = 0;
           var tableRef = document.getElementById("myList").getElementsByTagName("tbody")[0];



           for (var i = 0; i < tableRef.rows.length; i++) {

               alert(tableRef.rows[i].cells[3].innerHTML.substr(1));

               var addBalance = parseInt(tableRef.rows[i].cells[3].innerHTML.substr(1));
               totalBalance = totalBalance + addBalance;
           }

           alert(totalBalance);
           var myText = result.text;
           var myTextTwo = myText.replace(/['"]+/g, '');
           alert(myTextTwo);
           var custBal;



           var myDB = window.sqlitePlugin.openDatabase({ name: "mySQLite.db", location: 'default' });

           alert("Hello");

           myDB.transaction(function(transaction) {
               alert("Hello");
               transaction.executeSql("SELECT customerBalance FROM customer where nric = '" + myTextTwo + "'", [], function (tx, results) {
                   var len = results.rows.length;
                   for (var i = 0; i < len; i++) {
                       custBal = results.rows.item(i).customerBalance;
                       alert("result row" + results.rows.item(i).customerBalance);
                       alert("CustomerBal" + custBal);
                   }
               },
                 function(tx, error)
                 {

                 }
                   );
           });



           alert("Total after entitled discount: ");
           alert("Transaction successful, Next Customer Please");

       }

1 个答案:

答案 0 :(得分:0)

在数据库事务在成功或错误处理程序中返回或失败后移动您想要发生的任何事情,我只是将它们从参数中移出并给出它们的名称以跟踪它们。然后将您的注释添加到successCallback处理程序的末尾。在事务未成功后您想要发生的任何事情都使用错误回调。

 function successCallback(tx, results) {
 var len = results.rows.length;
 for (var i = 0; i < len; i++) {
     custBal = results.rows.item(i).customerBalance;
     alert("result row" + results.rows.item(i).customerBalance);
     alert("CustomerBal" + custBal);
 }


 alert("Total after entitled discount: ");
 alert("Transaction successful, Next Customer Please");
 }

 function errorCallback(tx, error) {

 }

 myDB.transaction(function (transaction) {
     alert("Hello");
     transaction.executeSql("SELECT customerBalance FROM customer where nric = '" + myTextTwo + "'", [], successCallback, errorCallback);
 });