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"');

Tarannum
  • 13
  • 4

2 Answers2

0

Try it;

var check = db.query(query1,query2);
Jessé Filho
  • 41
  • 1
  • 7
  • 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. – skomisa Jul 27 '21 at 17:42
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);
});
ikhvjs
  • 5,316
  • 2
  • 13
  • 36
  • 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 Jul 28 '21 at 12:26
  • @Tarannum, I updated my answer. Please check if you have any errors from it. If so, please provide the error. – ikhvjs Jul 28 '21 at 12:30
  • 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 – Tarannum Jul 28 '21 at 12:40
  • 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 Jul 28 '21 at 12:42
  • @Tarannum, can you please just copy the whole script from my answer to run? including the `const query1= .....` – ikhvjs Jul 28 '21 at 12:43
  • @Tarannum, A hint, try to run the SQL in your DB to see if there is any Error. Make sure the SQL is working in DB first. – ikhvjs Jul 28 '21 at 12:50
  • A suggestion, How about just set the two querys as a simple select SQL? Any error? – ikhvjs Jul 28 '21 at 13:13
  • var check = await db.query(`${'select id from test'} ;${' select * from test'}`); thjis is my query pls check this issue is there any syntax issue – Tarannum Jul 28 '21 at 13:17
  • @Tarannum, try like this `var check = await db.query("select id from test; select * from test");` without the `${}` – ikhvjs Jul 28 '21 at 13:18
  • @Tarannum, Please update your question with all the codes you are using. It is hard to help you debug without all the codes. – ikhvjs Jul 28 '21 at 13:33
  • Yes I update my question with full code pls check – Tarannum Jul 29 '21 at 06:55
  • @Tarannum, very good. I updated the answer with your connection codes. – ikhvjs Jul 29 '21 at 07:37
  • @Tarannum, It is good to hear at least query1 is working now. I updated my answer with two simple select and console.log(query) later in the db.js. Can you please try and show me console.log(res) in App.js and the console.log(query) in the db.js? – ikhvjs Jul 29 '21 at 08:29
  • this is the result of console.log(res) :- [ RowDataPacket { '1': 1 } ] – Tarannum Jul 29 '21 at 08:37
  • when I add this const query1 = query.join(";"); console.log({ query1 }); then error is showing : - query.join is not a function – Tarannum Jul 29 '21 at 08:40
  • @Tarannum, why do you change the codes? please copy the whole `exports.query` in your db.js and show the result of `console.log({ query })` but not `query1`. – ikhvjs Jul 29 '21 at 08:47
  • Ok now this error arise :- { query: 'SELECT 1;SELECT 2' } (node:14556) 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 'SELECT 2' at line 1 – Tarannum Jul 29 '21 at 09:14
  • @Tarannum, we can see the query is correct and just to be sure, did you add `multipleStatements: true,` in your `db_config` ? I found a related issue of this https://stackoverflow.com/a/58411423/14032355. – ikhvjs Jul 29 '21 at 09:23
  • @Tarannum, good to hear. Can you please accept this answer if you find this useful and it may help people with similar issue in the future. – ikhvjs Jul 29 '21 at 09:54