简体   繁体   中英

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). I would like to add a openingTime, closingTime, and phoneNumber...but I want them added via an storeId, which already exists in the table. Do I need to make an individual call for each entry based on storeId. Or is it possible for me to make a single call to the table and loop through each 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). 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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