0

Im working on a project with Nuxt.Js, Express and MySQL.

Im trying to insert data to two tables using MySQL transactions, explained here.

Here is an example of my code:

app.post('/temp', function (req, res) {
  var sql = "BEGIN; INSERT INTO temp (number) VALUES (2); INSERT INTO project (name) VALUES ('test'); COMMIT;";
  connection.query(sql, function (err, rows) {
    if (err) throw err
    res.send(rows)
  })
})

The

INSERT INTO temp (number) VALUES (2)

and

INSERT INTO project (name) VALUES ('test')

work fine on their own so it seems there is a problem with the transaction syntax?

I'm getting an "ER_PARSE_ERROR" error which to my understanding means that there is simply and error with MySQL syntax.

enter image description here

And why I need to do it in one sql query is because I have a situation similiar to this.

EDIT -------------------------------------------------------------------------------------------------------

The query works fine in localhost/phpmyadmin so the problem might actually not be in the sql query.

Nico Haase
  • 9,476
  • 35
  • 35
  • 57
  • 2
    You cannot use multiple SQL statements in one line. kind of like [this](https://stackoverflow.com/questions/23266854/node-mysql-multiple-statements-in-one-query). If you want to, you should use: `multipleStatements: true` – Luuk Feb 14 '22 at 09:46

0 Answers0