将mariadb nodeJS连接器与多个查询一起使用

时间:2019-01-08 15:55:52

标签: node.js mariadb

我有一台运行Node.js的服务器连接到MariaDb数据库。我正在使用Node.js connector,它对于单个查询工作正常。但是,当我尝试执行多个查询时,会抛出此错误:

{ Error: (conn=8439, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 2' at line 1  
sql: SELECT 1; SELECT 2; - parameters:[]
 at Object.module.exports.createError (\node_modules\mariadb\lib\misc\errors.js:55:10)
 at Packet.readError (\node_modules\mariadb\lib\io\packet.js:506:19)
 at Query.readResponsePacket (\node_modules\mariadb\lib\cmd\resultset.js:47:28)
 at PacketInputStream.receivePacket (\node_modules\mariadb\lib\io\packet-input-stream.js:73:9)
 at PacketInputStream.onData (\node_modules\mariadb\lib\io\packet-input-stream.js:129:20)
 at Socket.emit (events.js:182:13)
 at addChunk (_stream_readable.js:283:12)
 at readableAddChunk (_stream_readable.js:264:11)
 at Socket.Readable.push (_stream_readable.js:219:10)
 at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
fatal: false,
errno: 1064,
sqlState: '42000',
code: 'ER_PARSE_ERROR' }

我正在使用的当前测试代码是:

conn = await pool.getConnection({multipleStatements: true});
conn.query({
  multipleStatements: true,
  sql: "SELECT 1; SELECT 2;"
}).then((data: any) => {
  console.log(data);
  conn.end();
});

documentation建议使用multipleStatements,但是从GitHub Repo看,我发现它唯一的位置是在lib/config/connection-options.js和{{3}下}。我还注意到了GitHub documentation,但这似乎是专门针对大量插入内容的。

有没有一种方法可以使用mariadb-connector-nodejs在单个query调用中运行多个查询?如果是这样,那么只有多语句查询无法正常工作,我还能做错什么?

版本:
-MariaDb:10.1.34-MariaDB
-Node.js:v10.14.2
-mariadb-connector-nodejs:mariadb@2.0.2-rc

1 个答案:

答案 0 :(得分:2)

multipleStatements is the good option, but as @rolandstarke indicate, that is a connection option.

When using creating pool, you indicate pool + connection options, since pool will handle connection creation. see related documentation

Example:

const mariadb = require("mariadb");
const pool = mariadb.createPool({ multipleStatements: true });

pool.query("select 1; select 2")
  .then(results => {
    //select 1 results
    console.log(results[0]); //{ '1': 1 }
    //select 2 results
    console.log(results[1]); //{ '2': 2 }
  })
  .catch(err => {
    //handle error
  });