【发布时间】:2018-01-14 11:32:11
【问题描述】:
当用户使用应用程序时,我的混合应用程序正在将数据从服务器推送到 cordova 应用程序。在我的服务器上,我的 JSON 中有 2000 个数据。我成功地将 2000 个数据插入到我的 SQLite 数据库中。但是,我意识到该程序插入了 2000 个与服务器中最后一个数据相同的数据。以下是我的代码。
$http.get("http://131.4.44.69/php3/fetchSAPInfo.php?key=" + lastSAPOID).success(function (data){
alert(lastSAPOID);
if(data == 1){
alert("data is up to date");
}else{
alert("data is updating......");
var i;
for(i=0; i<data.length; i++){
var SAP_OID = data[i].SAP_OID;
var SAP_Asset = parseInt(data[i].SAP_Asset);
var SAP_Description = data[i].SAP_Description;
db.transaction(function(tx) {
alert(SAP_Asset);
tx.executeSql('INSERT INTO SAPInfo VALUES (?,?,?)', [SAP_OID, SAP_Asset , SAP_Description]);
}, function(error) {
console.log(error);
}, function() {
alert("insert successfully");
})
}
}
}).then (function (response){
alert("done and finished");
});
我意识到代码,转到 then 函数首先只运行 db.transaction() 函数。因此程序不会相应地获取我的 for 循环的 JSON 数据。我该如何解决这个问题?
【问题讨论】: