I use node-mysql, node-js and Q promises.
I have successfully updated, deleted and inserted individual rows using the above. As well as multiple lines in one expression in my scripting scripts.
However, I need to update several lines with different values (batch mode) either in a single request or in a for loop.
The information on how to use prepared statements in mysql2 (presumably improved on node-mysql) is very scarce and there are no examples, although this should be a natural choice, as well as promises to compensate for node-js asynchronous.
In addition, I have successfully used defered.makeNodeResolver () in various test cases.
I am trying to update multiple rows in a single query with a where clause and changing conditions.
It works when I update one line. However, when I try to update multiple rows with a single query, the records are not updated.
I am ready to move on to using the for loop to perform several updates, and then aggregate the result and send it back from the server to the client, which would be my second preferred choice. And I do not understand why this should not be done in this way, if there are not too many impacts on performance. But I did not find examples for this.
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 does not actually update multiple lines. I assume there is an error in my syntax.
But anyway, what is the best approach for this in this particular scenario? Prepared statements, repeated requests in a for loop, or stored procedures?