我要检查数组中的值是否在数据库中,如果没有,则将其添加到数据库中?
我正在使用node.js进行编程,并且具有一个“ .js”脚本,该脚本可以解析HTML文件中的数据并将记录添加到数组中。
这些值存储在名为“ json”的数组中,并且是字符串,
该数据库是MySQL数据库,我使用node.js程序中的模块“ MySQL”使用xampp / PHPMyAdmin查看数据。
下面使用的函数是一个异步函数,它将记录(如果有)返回到结果变量。
检查底部的伪代码以清楚了解我要执行的操作。
对不起,英语不好:)
我遇到的问题是事实是“从数据库获取记录” /“con.query”函数运行,然后在程序结束之前没有时间实际获取值,因为异步
我已经尝试过等待并承诺,但经验不足,想学习。
我知道解决方案充满期待和希望,但我已经尝试了数小时,也进行了数小时的研究。
我需要在程序的其余部分之前完成此功能。
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
}
}
使用此循环检查项目是否在数据库中,如果没有,则添加它:
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函数。
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”是我具有需要在其中检查的记录的数组的名称。
找到要发布到下面的解决方案。
答案 0 :(得分:1)
for (var i = 0; i < json.length; i++) {
var sql = "SELECT * FROM `table` WHERE `table_name` = '" + json[i] + "'";
con.query(sql, function(err, result, fields) {
if (err) {
throw err;
}
if (!result[0]) {
var sql = "INSERT INTO `table` VALUES('" + json[i] + "')";
con.query(sql, function(err, result, fields) {
if (err) {
throw err;
}
});
}
});
}
希望能奏效
答案 1 :(得分:0)
json = [your array];
async function check_database() {
try {
console.log("Checking JSON For New Records");
//This is a single multi-query, I enter my long string of all queries ready to be checked.
var sql = await check_records();
insert_new_records(sql);
console.log("Finished Checking JSON For New Records \n");
}
catch(error) {
console.error(error);
console.log(" \n FAILED CHECKING RECORDS FOR NEW ITEMS \n ");
}
}
async function check_records() {
return new Promise(function(resolve, reject) {
//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] + '"; ';
}
connection.query(sql, [1, json.length], function(err, results) {
var sql = "";
//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++){
var str = JSON.stringify(results[i]);
if( str.length < 3 )
{
//If the record has a length then it's returned a record so it's added.
sql += "INSERT INTO table (table_name) VALUES ('" + json[i] + "'); ";
notify_user(json[i]);
} else {
}
}
resolve(sql)
});
})
}
check_database();