Using prepared statements with bulk query() operations.
Hi There,
From my understanding the following bulk insert does not use prepared statements:
var mysql = require('mysq2');
var conn = mysql.createConnection({
...
});
var sql = "INSERT INTO Test (name, email, n) VALUES ?";
var values = [
['demian', '[email protected]', 1],
['john', '[email protected]', 2],
['mark', '[email protected]', 3],
['pete', '[email protected]', 4]
];
conn.query(sql, [values], function(err) {
if (err) throw err;
conn.end();
});
I have two questions:
1- How safe are bulk operations with query()? Is there still room for sql injection attacks?
2- Would it be possible to make execute() support bulk operations just like query() while still using prepared statements?
Would it be possible to make execute() support bulk operations just like query() while still using prepared statements?
it's possible, but I guess not very efficient - it would require preparing statement with as much parameters as values in input data
Have you tried load data local infile? Have a look at example at https://github.com/sidorares/node-mysql2/blob/master/documentation/Extras.md#sending-tabular-data-with-load-infile-and-local-stream
I suspect it's more efficient and less prone to sql injection compared to large string constructed from INSERT INTO Test (name, email, n) VALUES + data.