【问题标题】:Javascript / Phonegap with SQLite insert bug?带有 SQLite 插入错误的 Javascript/Phonegap?
【发布时间】:2014-10-14 21:21:18
【问题描述】:

我正在构建一个目前主要针对 iOS 7+ 的 Phonegap 应用程序。

我有一个本地 SQLite 数据库,我正在从服务器复制干净。此时有几个表是空的(它们并不总是空的)。当我使用下面的代码时,result.insertId 值不会被填充,而只是插入到表中的第一行。在第一行之后,所有insertId值都是正确的。

db.transaction(function (tx1) {
   tx1.executeSql(insertSQL1, [arguments],
      function (t1, result1) {
          if (result1 != null && result1.rowsAffected > 0) {
              logMessage("Result1:" + JSON.stringify(result1));
              $.each(UserSelectedArrayOfItems, function (index, value) {
              db.transaction(function (tx2) {
                    tx2.executeSql(insertSQL2, [result1.insertId, value],
                         function (t2, result2) {
                              if (result2 != null) {
                                  logMessage("Result2: " + JSON.stringify(result2));
                              }
                         },
                         function (t2, error) {
                              logMessage("There was an error: " + JSON.stringify(error));
                         });
                    });
               });
              << Do app Navigation if all was ok >> 
          }
      },
      function (t, error) {
           logMessage("Error: " + JSON.stringify(error));
      });
});

在测试时,两个表都开始为空。零行。两者都有一个带有属性的 ID 列:INTEGER PRIMARY KEY。插入确实有效,并且确实获得了 ID 1。该 ID 在表中是正确的,但在事务成功回调中未定义 result.insertId

logMessage 函数将字符串写入文件,以便我可以调试/支持应用程序。将 1 行插入到父表(始终只有 1 行到父表)和 2 行到子表(可能是 1 到 n 行)时的示例日志消息:

Result1: {"rows":{"length":0},"rowsAffected":1}
Result2: {"rows":{"length":0},"rowsAffected":1}
Result2: {"rows":{"length":0},"rowsAffected":1,"insertId":2}

以前有人见过这种行为吗?我正在使用以下插件:

<gap:plugin name="com.millerjames01.sqlite-plugin" version="1.0.1" />

【问题讨论】:

  • 我注意到您在外部 tx.executeSql() 调用中使用 db.transaction()。您可以在外部调用中使用 tx.executeSql() 。也许这就是问题所在。
  • 这个问题解决了吗?

标签: javascript sqlite cordova insert


【解决方案1】:

是的,我发现当您使用结果(或行)中的值时,最佳做法是首先将对象的值评估为新变量,然后将新变量传递给另一个函数调用并完成工作那里。作为对我的示例的更新:

db.transaction(function (tx1) {
   tx1.executeSql(insertSQL1, [arguments],
      function (t1, result1) {
          if (result1 != null && result1.rowsAffected > 0) {
              logMessage("Result1:" + JSON.stringify(result1));
              var newID = result1.insertId;
              $.each(UserSelectedArrayOfItems, function (index, value) {
                DoWork(newID, value);
               });
              << Do app Navigation if all was ok >> 
          }
      },
      function (t, error) {
           logMessage("Error: " + JSON.stringify(error));
      });
});


function DoWork(id, value){
   db.transaction(function (tx2) {
        tx2.executeSql(insertSQL2, [id, value],
             function (t2, result2) {
                  if (result2 != null) {
                      logMessage("Result2: " + JSON.stringify(result2));
                  }
             },
             function (t2, error) {
                  logMessage("There was an error: " + JSON.stringify(error));
             });
        });
    }

您需要添加任何支票或退货,以便确认一切都按预期进行。但这就是为我解决问题的总体要点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多