mysql - 通过 node.js 向 mysql 中插入多行

标签 mysql node.js node-mysql

我想通过 node.js mysql 模块将多行插入到 mysql 中。我的数据是

var data = [{'test':'test1'},{'test':'test2'}];

我正在使用池

 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();
            }
      });
 });
}

失败了。

有没有办法让我批量插入并在所有插入完成后调用一个函数?

问候 锤子

最佳答案

你也可以试试这个方法

假设 mytable 包括以下列:name, email

var inserts = [];
inserts.push(['name1', 'email1']);
inserts.push(['name2', 'email2']);
conn.query({
sql: 'INSERT into mytable (name, email) VALUES ?',
values: [inserts]
});

这应该可行

关于mysql - 通过 node.js 向 mysql 中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24348999/

相关文章:

python - 如何修复 'MySQLConverter' 对象没有库存输入的属性 '_entry_to_mysql',python-mysql?

angularjs - 如何修复 npm UNMET PEER DEPENDENCY 警告?

mysql - 无法将nodejs连接到mysql

mysql - 通过 ssl 连接到 mysql 的最佳 Node.js mysql 模块是什么?

mysql - 无法使用 mysql 增加空集

php - 计算不同的行数

javascript - 在 electronjs 中,在主进程中然后在渲染进程中使用 child_process.fork 有什么好处

javascript - 对象未定义但 console.log 显示一个对象

Node.js mysql 客户端未定义

mysql - 选择非重复记录