【发布时间】:2019-11-29 23:00:53
【问题描述】:
总结
我想检查我的数组中是否有任何值在数据库中,如果没有,则将它们添加进去?
我正在使用 node.js 进行编程,并且有一个“.js”脚本可以解析 HTML 文件中的数据并将记录添加到数组中。
这些值存储在一个名为“json”的数组中,并且是字符串,
该数据库是一个 MySQL 数据库,我使用 xampp/PHPMyAdmin 来查看其中的数据,在我的 node.js 程序中使用模块“MySQL”。
我使用的函数如下,它是一个异步函数,将记录(如果有的话)返回给结果变量。
检查底部的伪代码,清楚地了解我正在尝试做什么。
抱歉英语不好:)
问题/问题
我遇到的问题是“get-record-from-the-database”/“con.query”函数运行,然后在程序结束之前没有时间实际获取值,因为它异步,
我尝试过 await 和 promises,但我非常缺乏经验并想学习。
我知道解决方案需要等待和承诺,但我已经尝试了几个小时,也研究了几个小时。
我需要在程序的其余部分之前完全完成该功能。
从数据库函数中获取记录
var json = ['value1', 'value2'];
var sql = "SELECT * FROM `table` WHERE `table_name` = '"+ json[i] +"'";
con.connect(function(err) {
con.query(sql, function (err, result, fields) {
console.log(result);
});
});
我希望我的数组中尚未添加到要添加的数据库中的任何记录,
这是因为程序会反复运行,所以我不想一遍又一遍地添加相同的记录,只添加新记录。
伪代码
LOOP FOR ALL RECORDS IN jsonArray
{
CHECK IF THE ARRAY RECORD IS IN DATABASE
IF(arrayRecord IS NOT IN database)
{
ADD arrayRecord INTO database
}
IF(arrayRecord IS IN database)
{
DO NOTHING
}
}
使用循环 - 更新 1
使用此循环检查该项目是否在数据库中,如果没有,则添加它:
for (var i = 0; i < json.length; i++)
{
var sql = "SELECT * FROM `table` WHERE `table_name` = '" + json[i] + "'";
con.connect(function(err) {
con.query(sql, function(err, result, fields) {
if (result == null)
{
var sql = "INSERT INTO table (table_name) VALUES('" + json[i] + "')";
con.query(sql, function(err, result) {
console.log("1 record inserted" + ":" + sql + ":" + i + ":" + json[i]);
});
}
});
});
}
我在使用循环时得到以下结果
console.log("1 record inserted" + ":" + sql + ":" + i + ":" + json[i]);
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
1 record inserted:INSERT INTO table (table_name) VALUES('undefined'):6:undefined
它似乎在循环中快速循环,而不检查上面的代码是否实际工作,然后当循环达到 6 时,它会触发所有 con.query 函数。
检查记录是否在数据库中的解决方案 - UPDATE 2
function check_database() {
//Define sql query text
var sql = "";
//compile all my queries for each record in array so that they can be .queried
for (var i = 0; i < json.length; i++){
sql += 'SELECT * FROM table WHERE table_name = "' + json[i] + '"; ';
}
//This is a single multi query, I enter my long string of all queries ready to be checked.
connection.query(sql, [1, json.length], function(err, results) {
if (err) throw err;
//Once done I need to cycle through the results array to check if any have returned a record hence are inside the db
for (var i = 0; i < json.length; i++){
//If the record has a length then it's returned a record so it's added.
if(!results[i].length)
{
//Otherwise No length means no record return, means no record in database
console.log("Record for: " + json[i] + " ||| NEEDS ADDING");
} else {
console.log("Record for: " + json[i] + " ||| IS A RECORD");
}
}
});
connection.end();
}
“json”是数组的名称,里面有需要检查的记录。
整个创建的最终解决方案 - 更新 3
在下面找到解决方案。
【问题讨论】:
-
每次循环时不要重新连接数据库
-
@AndrewKachnic 我找到了一个解决方案,我知道这不是最好的方法,但它确实有效。你能想出更好的方法吗?
标签: mysql node.js function loops asynchronous