MySQL 通过node.js向mysql中插入多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24348999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert multiple rows into mysql through node.js
提问by Hammer
I want to insert multiple rows into mysql thru node.js mysql module. The data I have is
我想通过 node.js mysql 模块将多行插入到 mysql 中。我拥有的数据是
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
问候锤子
采纳答案by Ben
You can insert multiple rows into mysql using nested arrays. You can see the answer from this post: How do I do a bulk insert in mySQL using node.js
您可以使用嵌套数组将多行插入到 mysql 中。您可以从这篇文章中看到答案: How do I do a bulk insert in mySQL using node.js
回答by Sul Aga
You can try this approach as well
你也可以试试这个方法
lets say that mytableincludes the following columns: name, email
假设mytable包括以下列:姓名、电子邮件
var inserts = [];
inserts.push(['name1', 'email1']);
inserts.push(['name2', 'email2']);
conn.query({
sql: 'INSERT into mytable (name, email) VALUES ?',
values: [inserts]
});
This should work
这应该工作