David Starkey
David Starkey

Reputation: 1840

Using mariadb nodeJS connector with multiple queries

I've got a server running Node.js connecting to a MariaDb database. I'm using the Node.js connector and it's working fine for single queries. However, when I try to do multiple queries it's throwing this error:

{ 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' }

The current test code I'm using is:

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

The documentation suggests multipleStatements is an option, but looking through the GitHub Repo, the only places I've found it is under lib/config/connection-options.js and the GitHub documentation. I also noticed the batch option but that seems to be specifically for a load of inserts.

Is there a way to use the mariadb-connector-nodejs to run multiple queries in a single query call? If so, what else could I be doing wrong that only multi-statement queries are not working?

Versions:
- MariaDb: 10.1.34-MariaDB
- Node.js: v10.14.2
- mariadb-connector-nodejs: [email protected]

Upvotes: 2

Views: 3958

Answers (1)

Diego Dupin
Diego Dupin

Reputation: 1348

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
  });

Upvotes: 9

Related Questions