0

I use "mysql": "^2.18.1" on NodeJs.

I have a query that resembles updateProperty: `UPDATE object SET property = ? WHERE id = ?;` . In code I have a lot of objects of each I want to update the property each x seconds so I thought I would just concat the query like:

let query = "";
let args = [];
for(const obj of objects){
    query += updateProperty;
    args.push(obj.property);
    args.push(obj.id);
}

and then use DB.query(query, args, (err: any) => { ... }) but I get You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version in this case.

I also tried replacing in the query directly with replace("?", obj.property) etc but encounter the same error.

Is there a "better" way to approach this?

Kiwi
  • 38
  • 5
  • Not sure if the '?' method works for multi-query, but I'm guessing you're just missing the ';' in your query string between each update query? Ex. `UPDATE x WHERE id = ?; UPDATE y WHER id = ?;` – Bert Maurau Sep 05 '20 at 22:30
  • i was missing `multipleStatements: true` in the createConnection :/ thank you all! – Kiwi Sep 05 '20 at 23:25
  • Requiring a regular interval update of a property looks wrong. Consider if there's a way to keep the way it is fixed and query it based on the current time. e.g. store the expire date/time and not the hours/minutes/seconds remaining. – danblack Sep 05 '20 at 23:52

0 Answers0