简体   繁体   中英

nodejs How to execute a mysql query with 2 statements

I'd like to execute 2 mysql statements with one query in node js. However this fails with the error:

ER_PARSE_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

My node command and mysql query looks as follows.

connection.query( "UPDATE `table1` SET `count` = `count` + 1 WHERE `id` = ? LIMIT 1; INSERT INTO `table2` (`id`, `field1`, `datetime`, `field2`, `field3`, `field4`) VALUES     (NULL, ?, NOW(), ?, ?, 'impression');", 
    [var1, var2, var3,var4],
    function(err, rows) {
        connection.release();
        if (err != null) {
            // Do error logging
            console.log(this.sql);
            console.log(err);
        }
    });

Taking the mysql query statement from the console log and executing it manually works perfectly fine. Thus the problem must be caused by node mysql.

How can I execute these two statements with one query in node js?

Thx, i really appreciate your expertise!

In node-mysql , support for multiple queries is disabled by default. To enable it, do it when creating your connection:

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

Take a look at the docs for more information.

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