2

我正在尝试遍历我的 Sqlite DB 中的所有表并将它们发送到服务器 DB。一切正常,除了当我到达通过 $.get() 发送信息的部分时,tbl_name 变量已经设置为最后一个值...... IE 所有 $.get() 调用都使用相同的值tbl_name,最后一个表名。

        var submissionID=10;
        var varStr;

        DEMODB.transaction(function (transaction) {
            transaction.executeSql("SELECT name FROM sqlite_master WHERE type='table';", [], function(transaction, results) {
                for (var i = 0; i < results.rows.length; i++) {
                    var tables = results.rows.item(i);
                    var tbl_name=tables['name'];

                    if (tbl_name=="__WebKitDatabaseInfoTable__" || tbl_name=="Submission") { continue; }

                    transaction.executeSql("SELECT * FROM " + tbl_name + " WHERE submissionID=?;", [ID], function(transaction, results) {
                        varStr="";
                        for (var i = 0; i < results.rows.length; i++) {
                            var row = results.rows.item(i);

                            for (col in row) {
                                if (col=="submissionID" || col=="ID") { continue; }
                                varStr += col + "=" + row[col] + '&';
                            }
                        }
                        varStr += 'submissionID=' + submissionID + "&table=" + tbl_name + "&num=" + i;
                        $.get("script/to/process.aspx",varStr);
                    }, errorHandler);
                }
            }, errorHandler);
        });
4

1 回答 1

1
DEMODB.transaction(function (transaction) {
  transaction.executeSql("SELECT name FROM sqlite_master WHERE type='table';", [], function(transaction, results) {
    for (var i = 0; i < results.rows.length; i++) {
      var tables = results.rows.item(i);
      handleTable(tables['name'], transaction);
    }
  }, errorHandler);
});


function handleTable(tbl_name, transaction) {

  if (tbl_name=="__WebKitDatabaseInfoTable__" || tbl_name=="Submission") { return; }

  transaction.executeSql("SELECT * FROM " + tbl_name + " WHERE submissionID=?;", [ID], function(transaction, results) {
    varStr="";
    for (var i = 0; i < results.rows.length; i++) {
      var row = results.rows.item(i);

      for (col in row) {
        if (col=="submissionID" || col=="ID") { continue; }
        varStr += col + "=" + row[col] + '&';
      }
    }
    varStr += 'submissionID=' + submissionID + "&table=" + tbl_name + "&num=" + i;
    $.get("script/to/process.aspx",varStr);
  }, errorHandler);
}
于 2012-11-20T05:31:49.083 回答