gpt4 book ai didi

javascript - MySQL 循环查询 Javascript 不起作用

转载 作者:行者123 更新时间:2023-12-03 11:34:22 26 4
gpt4 key购买 nike

我在这个循环中使用了闭包。但它只在控制台日志上打印正确的数据,并且sql查询不起作用。 MySQL上插入的数据是循环的最后一个变量。我认为这是因为MySQL的写入速度。但不知道如何解决。任何想法?谢谢

  module.exports = function (callback) {
queryGetForSend = "SELECT * FROM image WHERE send_request is NULL AND post_request is NOT NULL AND year(moderate_time) = year(curdate()) AND month(moderate_time) = month(curdate()) AND (time(moderate_time) < (curtime() - 15));";
conn.query(queryGetForSend, function(err, rows, fields){
for (i in rows) {
if (rows[i].post_request == 'approve') {
resultSend = 1
} else {
resultSend = 2
}
var fileID = rows[i].img_md5;
queryString = fileID + "=" + resultSend;

// Request url: "http://im-api1.webpurify.com/image_queue/results/?key="
var d = new Date();
Date.masks.default = 'YYYY-MM-DD hh:mm:ss';
sendTime = d.format();

(function(queryString, sendTime) {
querySent = "UPDATE image SET send_request=1,result_sent='"+queryString+"',send_time='"+sendTime+"' WHERE send_request is NULL AND post_request is NOT NULL AND year(moderate_time) = year(curdate()) AND month(moderate_time) = month(curdate()) AND (time(moderate_time) < (curtime() - 15));";
conn.query(querySent, function (err, rows, fields) {
if (err) throw err;
console.log("http://google.com?key=" + key + "&" + queryString);
});
})(queryString, sendTime);

(function(queryString){
request.get("http://google.com" + key + "&" + queryString, function(err, res, body) {
});
})(queryString);
}
// callback(rows);
});
};

最佳答案

两个建议:

  • 避免在 SQL 查询中串联,而是使用占位符或准备好的语句(如果您关心安全性)。

  • 使用array.forEach()代替带有闭包的常规for循环,以避免意外使用for循环内设置的变量:

    conn.query(queryGetForSend, function(err, rows, fields) {
    if (err) throw err;
    rows.forEach(function(row) {
    var resultSend;
    if (row.post_request == 'approve') {
    resultSend = 1
    } else {
    resultSend = 2
    }
    var fileID = row.img_md5;
    var queryString = fileID + '=' + resultSend;

    var d = new Date();
    Date.masks.default = 'YYYY-MM-DD hh:mm:ss';
    var sendTime = d.format();

    var querySent = 'UPDATE image SET send_request=1, result_sent=?, send_time=? WHERE send_request IS NULL AND post_request IS NOT NULL AND year(moderate_time) = year(curdate()) AND month(moderate_time) = month(curdate()) AND (time(moderate_time) < (curtime() - 15));';
    conn.query(querySent, [queryString, sendTime], function (err) {
    if (err) throw err;
    console.log('http://google.com?key=' + key + '&' + queryString);
    });

    request.get('http://google.com?key=' + key + '&' + queryString, function(err, res, body) {
    });
    });
    });

关于javascript - MySQL 循环查询 Javascript 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26586027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com