How do I do a bulk insert in MySQL using Node.js

Bulk inserts are possible by using nested array, see the github page

Nested arrays are turned into grouped lists (for bulk inserts), e.g. [['a', 'b'], ['c', 'd']] turns into ('a', 'b'), ('c', 'd')

You just insert a nested array of elements.

An example is given in here

var mysql = require('mysql');
var conn = mysql.createConnection({
  host: 'localhost',
  user: 'username',
  password: 'password',
  database: 'mydatabase' }); conn.connect();
var sql = "INSERT INTO Test (name, email, n) VALUES ?"; var values = [ ['demian', 'demian@gmail.com', 1], ['john', 'john@gmail.com', 2], ['mark', 'mark@gmail.com', 3], ['pete', 'pete@gmail.com', 4] ]; conn.query(sql, [values], function(err) { if (err) throw err; conn.end(); });

 

Source (Stackoverflow)

http://stackoverflow.com/questions/8899802/how-do-i-do-a-bulk-insert-in-mysql-using-node-js

 

posted @ 2016-03-22 16:49  壹加壹先生  阅读(424)  评论(0)    收藏  举报