Updating multiple rows with node-mysql, NodeJS and Q

╄→尐↘猪︶ㄣ 提交于 2019-11-30 13:55:35

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

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!