简体   繁体   English

如何使用节点js在mysql中使用数组更新多行

[英]How do I update multiple rows using an array in mysql using node js

I'm trying to use UPDATE to update 3 different columns in my table(stores). 我正在尝试使用UPDATE更新我的表(存储)中的3个不同的列。 I would like to add a openingTime, closingTime, and phoneNumber...but I want them added via an storeId, which already exists in the table. 我想添加一个openingTime,closeingTime和phoneNumber ...,但是我希望它们通过表中已经存在的storeId添加。 Do I need to make an individual call for each entry based on storeId. 我是否需要根据storeId对每个条目进行单独调用。 Or is it possible for me to make a single call to the table and loop through each storeId? 还是我可以对表进行一次调用并遍历每个storeId?

Here's what I have so far. 到目前为止,这就是我所拥有的。

var arraythatcontainsEverything = [];

var locations = ["randomA", "randomB", "randomC", ];
var openingTime = ["10:00", "10:00", "10:00"];
var closingTime = ["24:00", "24:00", "24:00"];
var phoneNumber = ["123-4567", "123-6789", "123-9999"];
var ids = ["210", "213", "234"];


var q = 'UPDATE stores SET openingTime=?, closingHours=?, phoneNumber=?  WHERE StoreId=?';
con.query(q, [array that contains everything], function(err, results){
if(err) console.log(err); 
if (DEBUG >= 2 )console.log("finished inserting hours and phone number into database");
                con.end();
)};

Support for multiple statements is disabled for security reasons (it allows for SQL injection attacks if values are not properly escaped). 出于安全原因,禁用了对多条语句的支持(如果值未正确转义,则允许进行SQL注入攻击)。 To use this feature you have to enable it for your connection: 要使用此功能,必须为连接启用它:

var connection = mysql.createConnection({multipleStatements: true});

Here is the MySQLJS Multiple Statement Queries Documentation 这是MySQLJS多语句查询文档

Once enabled, you would have to build a (multiple statement) query string. 启用后,您将必须构建(多条语句)查询字符串。

Its really easy, if you are going to loop to build each independent query and append it to the final query string, I would suggest using underscore's each. 这真的很容易,如果您要循环构建每个独立的查询并将其附加到最终查询字符串中,我建议使用下划线的每个。

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

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