简体   繁体   English

使用MySQL和Node.js更新查询

[英]Update query using MySQL and Node.js

I am using Node.js with MySQL. 我在MySQL上使用Node.js。 Everything works fine, however I am having issues when using the UPDATE query. 一切正常,但是使用UPDATE查询时出现问题。

My database table looks like this:

id     | employeeId
54027  | 8241
63708  | 3421
393835 | 3687

To check for external updates, I am sending a request to an API using the following code: 为了检查外部更新,我正在使用以下代码向API发送请求:

const obj = response.data.map(function(item) {
        return { id: item.entryId.id, employeeId: item.employeeId.id };
});

A console.log of this, returns the following: 的console.log返回以下内容:

[ { id: 54027, employeeId: 8242 },
  { id: 63708, employeeId: 3422 },
  { id: 393835, employeeId: 3688 }
]

So, since my API call has returned data which is different than what is in my DB, I want to issue an UPDATE query to MySQL, using the following code: 因此,由于我的API调用返回的数据与数据库中的数据不同,因此,我想使用以下代码向MySQL发出UPDATE查询:

const query = connection.query('UPDATE entries SET employeeID = ? WHERE entrieId = ?', obj,
(error, results, fields) => {
    if (error) console.log(error);
    else {
        console.log('Updated!');
    }
});

I get an error saying that I am using the wrong SQL syntax, then I get this at the bottom: 我收到一条错误消息,说我使用了错误的SQL语法,然后在底部显示了此错误:

UPDATE entries SET employeeID = id = 54027, employeeId = 8242 WHERE entrieId = id = 63708, employeeId = 3422 UPDATE entries SET employeeID = id = 54027, employeeId = 8242 WHERE entrieId = id = 63708, employeeId = 3422

I have been following tutorials and other advice found on forums / documentation for the best and correct way to issue the UPDATE, and am getting confused by my result. 我一直在遵循在论坛/文档中找到的教程和其他建议,以获取发布UPDATE的最佳和正确方法,并且对我的结果感到困惑。

let query = '';
for(var i = 0, len = obj.length; i < len; i++) {
query += 'UPDATE entries SET employeeID ='+obj[i]['employeeID']+' WHERE entrieId = '+obj[i]['id']+';'
}
 connection.query(query,
(error, results, fields) => {
    if (error) console.log(error);
    else {
        console.log('Updated!');
    }
});

When formatting SQL queries, JSON objects are turned into key = 'val' pairs. 格式化SQL查询时,JSON对象会变成key = 'val'对。 Have a look at https://github.com/mysqljs/sqlstring#escaping-query-values . 看看https://github.com/mysqljs/sqlstring#escaping-query-values Consider the following code, 考虑以下代码,

var data = {email: "e@example.com", status: 2} // consider some dummy data
var formattedQ = SqlString.format("UPDATE users SET ? WHERE id = 34", data);

// formattedQ would be:
"UPDATE users SET `email` = 'e@example.com', `status` = 2 WHERE id = 34"

For your problem you should use an array of values containing employeeId and id . 对于您的问题,您应该使用包含employeeIdid的值数组。

var data = [8242, 54027] // 8242 is employeeId and 54027 is id.
var q = "UPDATE entries SET employeeID = ? WHERE entrieId = ?";
connection.query(q, data, (err, results, fields) => {
    // Your code here
});

// This would execute the following formatted query:
UPDATE entries SET employeeID = 8242 WHERE entrieId = 54027

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM