简体   繁体   English

nodejs如何使用2个语句执行mysql查询

[英]nodejs How to execute a mysql query with 2 statements

I'd like to execute 2 mysql statements with one query in node js. 我想在节点js中用一个查询执行2个mysql语句。 However this fails with the error: 然而,这失败了,错误:

ER_PARSE_ERROR: You have an error in your SQL syntax; ER_PARSE_ERROR:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法

My node command and mysql query looks as follows. 我的node命令和mysql查询如下所示。

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. 从控制台日志中获取mysql查询语句并手动执行它完全正常。 Thus the problem must be caused by node mysql. 因此问题必须由节点mysql引起。

How can I execute these two statements with one query in node js? 如何在节点js中使用一个查询执行这两个语句?

Thx, i really appreciate your expertise! 谢谢,我非常感谢您的专业知识!

In node-mysql , support for multiple queries is disabled by default. node-mysql中 ,默认情况下禁用对多个查询的支持。 To enable it, do it when creating your connection: 要启用它,请在创建连接时执行此操作:

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

Take a look at the docs for more information. 查看文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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