如何使用nodejs在mariadb中批量插入?

时间:2016-06-09 07:54:27

标签: javascript node.js mariadb mariasql

每当我尝试运行我的代码时,这表明列数没有匹配错误。

values=[
    [{id:12227722345,name:"dgssssdavgsgfv",pass:"cvhsssssadfvugod"}],
    [{id:12,name:"ddd",pass:"cvh"}]
   ];

c.query('insert into Hash.asn(userid,username,password) values (?,?,?)',[values],function(err,rows)
{
  if (err)
    console.log(err);

  c.query('commit');
  console.log(rows);

}); 

错误:

{ [Error: Column count doesn't match value count at row 1] code: 1136 }

2 个答案:

答案 0 :(得分:1)

万一仍有人怀疑您可以使用connection.batch()进行批量查询。

connection.beginTransaction();
connection.query("INSERT INTO BASKET(customerId) values (?)", [1], (err, res) => {
   //must handle error if any
   const basketId = res.insertId;
   try {
     connection.batch("INSERT INTO basket_item(basketId, itemId) VALUES (?, ?)",[
         [basketId, 100],
         [basketId, 101],
         [basketId, 103],
         [basketId, 104],
         [basketId, 105]
     ]);
     //must handle error if any
     connection.commit();
     } catch (err) {
     connection.rollback();
     //handle error
     }
});

https://github.com/MariaDB/mariadb-connector-nodejs/blob/master/documentation/batch.md

答案 1 :(得分:-1)

这些天,据我所知,mariaDB模块不支持在node.js上进行批量插入。

https://mariadb.com/kb/en/library/connectornodejs-pipelining/