15

I am using node-mysql, node-js, and Q promises.

I have successfully updated, deleted, and inserted single rows using the above. As well as inserted multiple rows in a single statement in my test case scenario.

However, I need to update multiple rows with different values (batch mode) either in a single query or in a for loop.

The information on how to use prepared statements in mysql2 (supposed to improve on node-mysql) is very sparse and there are no examples, although that should be the natural choice, together with promises to compensate for node-js asynchronous nature.

In addition, I have successfully used defered.makeNodeResolver() in various test scenarios.

I am trying to update multiple rows in a single query with a where clause and changing conditions.

It is working when I update a single row. However, when I try to update multiple rows with a single query, the records aren't updated.

I am ready to switch to using a for loop to perform multiple updates and then aggregate the result and send it back from the server to the client, which would have been my second preferred choice. And I don't see why it shouldn't be done that way if there isn't too much of a performance hit. But I haven't found any examples for doing it that way.

var values = [
    { users: "tom", id: 101 },
    { users: "george", id: 102 }
    ];

    // var params = [values.users, values.id ];

    var sql = 'UPDATE tabletest SET users = ? WHERE id = ?;';


    connection.query(sql, [{users: values[0].users}, {id: values[0].id }], defered.makeNodeResolver());

The code shown above isn't actually updating multiple rows. I imagine there's an error in my syntax.

But anyway, what is the best approach to go about this in this particular scenario? Prepared statements, repeated queries in a for loop, or stored procedures?

Kis Leye
  • 161
  • 1
  • 2
  • 3
  • 1
    Did you try just using `[values[0].users,values[0].id]` as the second argument to `query()`? – mscdex Aug 28 '14 at 15:09
  • You were correct. I caught a syntax error. Thanks. I changed to the following: var sql = 'UPDATE tabletest SET ? WHERE ?;'; and var query = connection.query(sql, [{users: values[0].users}, {id: values[0].id }], defered.makeNodeResolver()); but it is still only updating one row and not both of them. Isn't it supposed to loop through the values objects? – Kis Leye Aug 28 '14 at 15:45
  • I'm not sure if that works for UPDATE, but it may work for INSERT. – mscdex Aug 28 '14 at 16:25
  • Apparently it will only update the first row. I wonder what the best approach out of this is? A for loop, prepared statements, or stored procedures, another alternative? Certainly someone has resolved this problem already. – Kis Leye Aug 28 '14 at 16:31
  • @KisLeye Did you find an efficient approach for this? Looping through and sending separate queries for each object doesn't sound like the right approach as most people here are saying that the call stack would max out at 10k records. – Ibrahim Farooq Jun 01 '21 at 19:16

7 Answers7

20

You can do it this way:

var values = [
  { users: "tom", id: 101 },
  { users: "george", id: 102 }
];
var queries = '';

values.forEach(function (item) {
  queries += mysql.format("UPDATE tabletest SET users = ? WHERE id = ?; ", item);
});

connection.query(queries, defered.makeNodeResolver());

To use multiple statements feature you have to enable it for your connection:

var connection = mysql.createConnection({
  ...
  multipleStatements: true,
});
Eugene
  • 655
  • 7
  • 16
  • But the author claim the mulitple statement feature is EXPERIMENTAL yet! I have not seen any feedback on 10K records update/insert in this way. – dialox Oct 14 '15 at 02:10
  • 1
    Has there been any developments on this answer? I am looking to do something similar tomorrow and am contemplating concatenating a query together also. – Filthy_Rich Jan 30 '17 at 23:57
  • If you are using DB String for creating connection, you can add `?multipleStatements=true` at end for adding options. Eg: `DATABASE_URL='mysql://user:password@domainame/dbname?multipleStatements=true'` – thisisjaymehta Dec 27 '22 at 04:30
3

Don't know if this problem it's still relevant but I will give my 2 cents:

The solution that I found is to use Promise.all

Basically you need to build an array of promises, where every promise is an update statement:

const getUpdatePromise = (row) => {
  return new Promise( (resolve, reject) => {
    mysqlConnection.query('UPDATE tableName SET foo = ? WHERE bar = ?', [row[0], row[1], (error, results, fields) => {
      if (error) reject(error)
      resolve(results)
    });
  })
}

You add this new Promise into an array and pass the final array with all the update statements to the Promise.all function.

It worked for me. Hope it works for someone else.

am1991
  • 134
  • 1
  • 7
  • 2
    If you are doing a lot of updates at once this will generate a lot of queries to your DB. It might be ok for you but you need to be aware of it. Limiting the number of parallel promises using a library like async might help. – Raphaël Verdier Jun 22 '22 at 12:23
1

You should probably do following way

UPDATE DB.table
SET table.row = newValue WHERE table.someColumn in ('columnVal1', 'columnVal2');

ex.

UPDATE DB.Students
SET result = "pass" WHERE rollNo in (21, 34, 50);
Sandip Nirmal
  • 2,283
  • 21
  • 24
1

enter image description here

let query = "";
query += "UPDATE `DBNAME` SET `field1` = `Value`, `field2` = CASE";
`Field3Array`.forEach((item, index) => {
 query += " WHEN `field3` = '" + item + "' THEN '" + `Field2Array[index]` + "'";
});
query += " END";
query += " WHERE `field3` IN (" + "'" + `Field3Array`.join("','") + "'" + ")";

sql.query(query, function (err, rows) {
    if (err) {
        console.log(err)
    } else {
        console.log(rows)
    }
});

In here, field1 & field 2 is needed be updated and field3 is 'where' clause. The result is undoubtedly success. I hope this answer will be of great help in updating multiple fields in multiple rows at once using mysql query.

Rabbit
  • 136
  • 1
  • 5
1

A simpler solution using multipleStatements: true:

 function multiupsert(table, cadena) {
return new Promise((resolve, reject) => {   


    var queries = '';
    cadena.forEach(function (item) {
         console.log(item)
        queries += `UPDATE ${table} SET descripcion = '${item.descripcion}',costo = '${item.costo}',estado = '${item.estado}' WHERE id = ${item.id} ; `
      });
      
      let conesM=getConectionMulti();
      conesM.connect();
      conesM.query(queries, (err1, result, fields) => {
        if (err1) return reject(err1)
            resolve(result.insertId);  
            console.log(result);
            conesM.end();   
        });

})

}

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

I don't think you can (at least easily/efficiently) update multiple rows in the way you are trying. You would basically have to loop over your values and execute an UPDATE for each object.

mscdex
  • 104,356
  • 15
  • 192
  • 153
0

this piece of code was taken from vcl.js for node.js, it is written in typescript and provides a multiple update,delete,inseer statment's in a single transaction.

export class SQLStatment {
    sql: string;
    params: Object
}

var dbError: string;
var execCount: number = 0;
function DBexecuteBatchMYSQLSingle(connection: any, SQLStatmentArray: Array<SQLStatment>, index: number, callback: () => void) {
    execCount++;
    connection.query(SQLStatmentArray[index].sql, SQLStatmentArray[index].params, function (err, rows, fields) {
        if (err) dbError = err;
        if (index + 1 == SQLStatmentArray.length) callback();
        else {
            if (!dbError) {
                DBexecuteBatchMYSQLSingle(connection, SQLStatmentArray, index + 1, function () {
                    if (index == 0) callback();
                });
            }
        }
    });
}

function DBBatchUpdateMYSQL(SQLStatmentArray: Array<SQLStatment>, callback?: (err) => void) {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: "host",user: "user",database: "db",
        port: 1022, password: "pass"});
    dbError = null;
    execCount = 0;
    connection.beginTransaction(function (err) {
        if (err && callback) callback("Database error:" + err);
        else {
            DBexecuteBatchMYSQLSingle(connection, SQLStatmentArray, 0, () => {
                if (dbError) {
                    connection.rollback(function () {
                        if (callback) callback("Database error:" + dbError);
                    });
                } else {
                    connection.commit(function (err) {
                        if (callback) callback(null);
                    })
                }
            });
        }
    });
}
Nati Krisi
  • 1,023
  • 1
  • 12
  • 23
  • the recursive call should work. But, if coping with 10K rows to update/insert, it might reach "Maxium callback stack" problem. – dialox Oct 14 '15 at 02:12