【问题标题】:Cordova SQLite, Push value from server and insert into SQLite DatabaseCordova SQLite,从服务器推送值并插入 SQLite 数据库
【发布时间】: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 数据。我该如何解决这个问题?

【问题讨论】:

    标签: android sqlite cordova


    【解决方案1】:

    我花了一整天的时间来找出问题所在。我的 for 循环必须放在 db.transaction() 函数中。这就是异步编程的问题。以下是我的答案。

          $http.get("http://131.4.44.69/php3/fetchSAPInfo.php?key=" + res).success(function (response){
                  if(response == 1){
                      console.log("data is up to date.......");
                  }else{
                      console.log("data is updating.........");
    
                      db.transaction(function(tx) {
                       var i;
                           for(i=0; i<response.length; i++){
                                var SAP_OID =  response[i].SAP_OID;
                                var SAP_Asset = parseInt(response[i].SAP_Asset);
                                var SAP_Description =  response[i].SAP_Description;
    
                                tx.executeSql('INSERT INTO SAPInfo VALUES (?,?,?)', [SAP_OID, SAP_Asset , SAP_Description]);
                           }
                       }, function(error) {
                              console.log(error);
                       }, function() {
                              console.log("insert successfully");
                       })
                }
        }).then (function (response){
              console.log("done and finished");
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2011-02-04
      • 1970-01-01
      相关资源
      最近更新 更多