1
In this only first query will run how I pass 2 query simultaneously .

I want to run insert or update query simultaneously.
db.js

require('dotenv').config()
const mysql = require('mysql');


const db_config = {
    connectionLimit: 10,
    host: "localhost",
    user: "root",
    password: "",
    database: "vechain"
};

const pool = mysql.createPool(db_config);
exports.pool = pool;
exports.query = function(query) {
    try {
        return new Promise((resolve, reject) => {
            pool.query(query, function(err, result, fields) {
                if (err) reject(err);
                resolve(result);
            });
        })
    } catch (err) {
        console.log('in db_sql function error');
        console.log(err);
        res.status(500).send({ success: false, msg: 'Error', data: '', errors: err });
    }
}

**app.js**

const db = require('./db'); //here I inclue database details

//this is my query var check = await db.query('insert into test ( name) Select "kk" where not exists(select * from test where name="kmkm" )';'update test set name = "88" where id = "463"');

7
  • what library are your using for the db object? Commented Jul 27, 2021 at 11:58
  • db is just a connection value Commented Jul 28, 2021 at 4:56
  • How do you initiate the db object then? Can you please show us? Commented Jul 28, 2021 at 5:43
  • var db= mysql.createConnection({ host: "localhost", user: "root_new", password: "pas", database: "database" }); Commented Jul 28, 2021 at 6:17
  • Sorry, Please also show us What is the query1 and query2 as well. I don't fully understand what are you trying to do in the SQL? Commented Jul 28, 2021 at 7:21

2 Answers 2

0

From the doc, you can pass two queries.

I add a option multipleStatements: true, in the config.

Also, I use ...args to pass multiple queries to the query function.

From the doc, Please note that the interface for streaming multiple statement queries is experimental

Example:

require("dotenv").config();
const mysql = require("mysql");

const db_config = {
  connectionLimit: 10,
  host: "localhost",
  user: "root",
  password: "",
  database: "vechain",
  multipleStatements: true,
};

const pool = mysql.createPool(db_config);
exports.pool = pool;
exports.query = function (...args) {
  try {
    const query = args.join(";");
    console.log({ query });
    return new Promise((resolve, reject) => {
      pool.query(query, function (err, result, fields) {
        if (err) reject(err);
        resolve(result);
      });
    });
  } catch (err) {
    console.log("in db_sql function error");
    console.log(err);
    res
      .status(500)
      .send({ success: false, msg: "Error", data: "", errors: err });
  }
};

You app.js

const db = require("./db"); //here I include database details

const query1 = "SELECT 1";
const query2 = "SELECT 2";

db.query(query1, query2).then(res => {
  console.log(res);
});
Sign up to request clarification or add additional context in comments.

19 Comments

var check = await db.query('insert into test ( name) Select "kk" where not exists(select * from test where name="kmkm" ) ; update test set name = "88" where id = "463"'); When I use this Then getting sql syntax error
@Tarannum, I updated my answer. Please check if you have any errors from it. If so, please provide the error.
this is my error UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: 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 'update test set name = "88" where id = "463"' at line 1 @ikhvjs
var check = await db.query(${'insert into test ( name) Select "kk" where not exists(select * from test where name="kmkm" )'} ;${ 'update test set name = "88" where id = "463"'});
@Tarannum, can you please just copy the whole script from my answer to run? including the const query1= .....
|
0

Try it;

var check = db.query(query1,query2);

1 Comment

To improve your answer and help the OP, please add an explanation for why this will work. As it stands, the brevity of your answer makes it liable for deletion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.