gpt4 book ai didi

mysql - node.js forEach 不阻塞

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:23 24 4
gpt4 key购买 nike

我试图通过提取 7 个字段并创建另一个表名与字段 1 匹配的表来处理 MySQL 表中的数据,其他 6 个字段是放入新创建表的字段中的数据。字段 1 是设备的标识符,我想记录对其他 6 个字段中其他信息的任何更改。例如...

表A

第 1 行 ---> 90:59:AF:4E:C3:30 |标记 | 10051 | PV320 | 192.168.1.199 | 1.35 | 026132956282

第 2 行 ---> 90:59:AF:55:A3:BA |芽 | 10050 | PV200 | 192.168.1.123 | 1.37 | 026132966540

我要创作

表 90:59:AF:4E:C3:30

第 1 行 ---> 标记 | 10051 | PV320 | 192.168.1.199 | 1.35 | 026132956282

和..表 90:59:AF:55:A3:BA

第 1 行 ---> 芽 | 10050 | PV200 | 192.168.1.123 | 1.37 | 026132966540

我遇到的问题是,我有大约 70 条记录,当我尝试运行我的代码时,我无法让它在创建和填充下一个表之前等待第一个表被创建和填充。我知道我的语法是正确的,因为它会在抛出“错误:ER_CON_COUNT_ERROR:连接太多”之前创建并填充多个表

这是我的代码:

function read_all_devices(){
var connection = mysql.createConnection({
host : db_host,
user : user,
password : pass,
database : my_db'
});
connection.query('SELECT macaddress, customer_name, ssh_port, validation_type, ip, version, serialnumber FROM ' + table + ' WHERE 1', function(err, rows) {
if (err) {
log(RED+err);
}
if (!err && rows[0]) {
rows.forEach(function(current){
var schema = mysql.createConnection({
host : db_host,
user : user,
password: pass,
database: 'information_schema'
});
schema.query("SELECT * FROM information_schema.tables WHERE table_schema = my_db AND table_name = '"+current.macaddress+"';", function(error, result) { //Check to see if the table already exists
if (error){
log("ERROR: " + error);
}
else if (result[0]) {
log("Table for " + current.macaddress + " already exists.");
update_historical_table(current.macaddress, current.customer_name, current.ssh_port, current.validation_type, current.ip, current.version, current.serialnumber);
}
else {
log("Table for " + current.macaddress + " did not exist....creating.");
create_table_by_mac(current.macaddress, current.customer_name, current.ssh_port, current.validation_type, current.ip, current.version, current.serialnumber, update_historical_table);
}
});
});
}
connection.end();
});

function create_table_by_mac(mac_to_create, name, port, val_type, ip, version, sn, printer, callback) {
var connection = mysql.createConnection({
host : db_host,
user : user,
password : pass,
database : my_db
});
connection.query("CREATE TABLE `" + mac_to_create + "` (recnum INT(10) NOT NULL AUTO_INCREMENT, customer_name VARCHAR(30) default NULL, ssh_port VARCHAR(6) default NULL, serialnumber VARCHAR(20) default NULL, validation_type VARCHAR(20) default NULL, ip VARCHAR(20) default NULL, version VARCHAR(20) default NULL, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (recnum));", function (create_error, create_result) {
if (create_error) {
log("ERROR Creating Table: "+ create_error);
} else {
log("Table " + mac_to_create + " created");
}
});
callback(mac_to_create, name, port, val_type, ip, version, sn);

function update_historical_table(mac_to_update, name, port, val_type, ip, version, sn) {
var connection = mysql.createConnection({
host : db_host,
user : user,
password : pass,
database : my_db
});
connection.query("INSERT INTO `"+ mac_to_update + "` (`customer_name`, `ssh_port`, `validation_type`, `ip`, `version`, `serialnumber`, `printer_type`) VALUES ('" + name + "', '" + port + "', '" + val_type + "', '" + ip + "', '" + version + "', '" + sn + "');", function(err, rows) {
connection.end();
if (!err) {
log(GREEN+"Table "+ WHITE + mac_to_update + GREEN + " updated successfully.");
} else if (err) {
log(RED+"ERROR Updating "+ WHITE + mac_to_update + ".");
log(RED+"ERROR MESSAGE: " + err);
}
});

这是我能粘贴的最好的代码。如果对我在这里尝试做的事情有任何疑问,请告诉我。

最佳答案

forEach 同步执行,但不会自动阻塞所有内容。如果您在该 block 内执行异步工作,forEach 不够智能,无法等到回调完成。看看 async图书馆,特别是eachSeries .它看起来像这样:

var async = require('async');
async.eachSeries(rows, functionToHandleEachRow, functionToBeCalledAfterEverythingIsFinished);

您需要查看 API 以了解有关迭代器和回调实现的更多详细信息。

关于mysql - node.js forEach 不阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21507080/

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