嵌套的sql查询里面的循环node.js无法正常工作

时间:2016-04-30 13:19:13

标签: javascript mysql json node.js

我有一个for循环通过JSON数组 - 在this stack question

的帮助下

循环脚本时会执行以下操作:

- Does the JSON item exist in the tables?
- if yes
    - Is the data different?
    - if yes
        - Update
    - if no
        - Do nothing
- if no
    -insert into database

我发现这个问题允许我查询每个for循环,这样我就能从JSON中获取名称并正确查询它们:

for(var i=0; i<content.prices.length; i++) {
        var price = content.prices[i].price
        var itemName = content.prices[i].market_hash_name;

        var q = "SELECT * FROM prices WHERE item = ?";
        (function() {
            var iNameCopy = itemName;
            var priceCopy = price;
            connection.query(q, iNameCopy, function (err, results) {
                if (err) throw err;
                if(results.length === 1) {
                    logger.info(iNameCopy + " does exist");
                }
                else if (results.length === 0){
                    logger.info(iNameCopy + " does not exist");
                }
            });
        }());
    }    

继续在results.length === 1

中添加if语句
for(var i=0; i<content.prices.length; i++) {
        var price = content.prices[i].price
        var itemName = content.prices[i].market_hash_name;

        var q = "SELECT * FROM prices WHERE item = ?";
        (function() {
            var iNameCopy = itemName;
            var priceCopy = price;
            connection.query(q, iNameCopy, function (err, results) {
                if (err) throw err;
                if(results.length === 1) {
                    if(parseFloat(results[0].current_price) != parseFloat(priceCopy)) { 
                       logger.info(iNameCopy + " does exist with old price: " + results[0].current_price + " and new price: " + priceCopy);
                   }
                }
                else if (results.length === 0){
                    logger.info(iNameCopy + " does not exist");
                }
            });
        }());
    }

这仍然有效。

现在最终用新的SQL查询替换那些记录器是代码绊倒的地方。 for循环运行就像我保持它们显示的记录器一样,但sql查询记录器不显示:

我尝试了这种方式,我从其他堆栈问题中发现并且没有将其包装在函数中,但都没有工作。有什么建议吗?

for(var i=0; i<content.prices.length; i++) {
        var price = content.prices[i].price
        var itemName = content.prices[i].market_hash_name;

        var q = "SELECT * FROM prices WHERE item = ?";
        (function() {
            var iNameCopy = itemName;
            var priceCopy = price;
            connection.query(q, iNameCopy, function (err, results) {
                if (err) throw err;
                if(results.length === 1) {
                    if(parseFloat(results[0].current_price) != parseFloat(priceCopy)) {
                        var sql2="UPDATE `prices` SET `current_price` = ?, `timeStamp` = ? WHERE `item` = ?";
                        (function() {
                            var iNameCopy2 = iNameCopy
                            var priceCopy2 = priceCopy
                            connection.query(sql2, [priceCopy2, 'Now()', iNameCopy2], function(err,results){
                                logger.info("UPDATE: " + iNameCopy2 + ' has been updated with price ' + priceCopy2);
                            });
                        }());
                    }
                }
                else if (results.length === 0){
                    var sql3="INSERT INTO `prices` (`item`, `current_price`, `timeStamp`) VALUES (?, ?, ?)";
                    (function() {
                        var iNameCopy3 = iNameCopy
                        var priceCopy3 = priceCopy
                        connection.query(sql3, [iNameCopy3, priceCopy3, 'Now()'], function(err,results){
                            logger.info("INSERT: " + iNameCopy3 + ' has been added with price ' + priceCopy3);
                        });
                    }());
                }
            });
        }());
    }

注意:我在for循环的外部有数据库连接:

var connection = mysql.createConnection(db_access);
    connection.connect(function(err){
      if(err){
        logger.error('Error connecting to Db');
        return;
      }
    });

0 个答案:

没有答案