我試圖查詢我的數據庫中的數據。但是,當我查詢我的數據時,我只能在我的最後一次警報(即alert("Total...")
)後獲取數據。例如,我運行下面的代碼,「result row:」警報僅在最後一次警報出現後才顯示。我可以知道爲什麼嗎?選擇查詢的順序不正確
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");
}
您需要回撥,即使數據庫尚未完成,代碼仍會繼續執行。你的sqlite pull的代碼在哪裏,特別是現在alert所創建的那一行。 –
@PatrickMurphy編輯。 – qcc