Sqlite和jquery变量在for循环设置中为最后一个值

时间:2012-11-20 05:09:32

标签: javascript jquery sqlite variables for-loop

我正在尝试遍历我的Sqlite数据库中的所有表并将它们发送到服务器数据库。一切正常,除非我到达通过$ .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);
        });

1 个答案:

答案 0 :(得分: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);
}