0

I'm using mysql2 in my nodejs project, in mysql library i used Multiple Statements for update my database and that works great, but for some reason in mysql2 it doesn't work, i googled it a lot and didn't found any solution, does mysql2 not support in multple statements??

  • Here's the multiple query

    async function updateChannels(dtz) {
    for (const data of dtz) {
      multipleQuery += `UPDATE myTable SET data='${data.newData}' WHERE data='${data.oldData}';`
    }
    await con.execute(multipleQuery);}
    

In mysql.createConnection i added multipleStatements: true

  • Here's the error

UnhandledPromiseRejectionWarning: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE

That query working great when i copy and paste it in the phpMyAdmin console

2 Answers2

4

You would need to use the query call instead of execute:

let rows, field;
try {
  [rows, fields] = await connection.query(query);
} catch (err) {
  // handle err
}
if (rows) {
  // exploit the result
}

But otherwise, yes, the execute command will let you run only a single instruction.

Will59
  • 1,430
  • 1
  • 16
  • 37
0

Resolved Issue on GitHub:

You can't prepare query with multiple statements. Split it in two or use non-prepared statements api ( dbpool.query(...) )

Time Killer
  • 693
  • 8
  • 21