Hi,
I want to insert multiple rows. The data I have is
var data = [{'test':'test1'},{'test':'test2'}];
I am using pool
pool.getConnection(function(err, connection) {
connection.query('INSERT INTO '+TABLE+' SET ?', data, function(err, result) {
if (err) throw err;
else {
console.log('successfully added to DB');
connection.release();
}
});
});
which fails.
Is there a way for me to have a bulk insertion and call a function when all insertion finishes?
Regards
Hammer
Hi! The reason that doesn't work is because in MySQL you cannot use INSERT INTO ... SET ... syntax for bulk inserts, the syntax is INSERT INTO table (col1,col2) VALUES(...). Here is how you would do bulk inserts with this module:
var data = [['test1'],['test2']];
connection.query('INSERT INTO table (??) VALUES ?', ['test',data], function(err, result) {
// ...
});
Most helpful comment
Hi! The reason that doesn't work is because in MySQL you cannot use
INSERT INTO ... SET ...syntax for bulk inserts, the syntax isINSERT INTO table (col1,col2) VALUES(...). Here is how you would do bulk inserts with this module: