最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to do bulk insert In mariadb using nodejs? - Stack Overflow

programmeradmin0浏览0评论

Whenever i am trying to run my code this is showing the column count doesn't match error.

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('mit');
  console.log(rows);

}); 

Error:

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

Whenever i am trying to run my code this is showing the column count doesn't match error.

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('mit');
  console.log(rows);

}); 

Error:

{ [Error: Column count doesn't match value count at row 1] code: 1136 }
Share Improve this question asked Jun 9, 2016 at 7:54 Akash Sourav NayakAkash Sourav Nayak 2031 gold badge9 silver badges21 bronze badges 10
  • I am not sure but just suggestion try with one value maybe the [] format is causing issue. – Neha Commented Jun 9, 2016 at 8:02
  • One value is working but i want to do with multiple value So can you help me how to do it? – Akash Sourav Nayak Commented Jun 9, 2016 at 8:04
  • just suggestion if its help. as i also not tried, why u keeping [] around the {} object ? any reason. if u remove it and try. – Neha Commented Jun 9, 2016 at 8:07
  • Then how will you send multiple values Like multiple id,multiple username at a time . – Akash Sourav Nayak Commented Jun 9, 2016 at 8:58
  • I think something like this - var values = [{1,'a','b'}, {2,'v','h'}]; insert into Hash.asn(userid,username,password) values (?,?,?)',[values] – Neha Commented Jun 9, 2016 at 9:41
 |  Show 5 more ments

2 Answers 2

Reset to default 9

In case anyone is still wondering this you can use connection.batch() to perform bulk queries.

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.mit();
     } catch (err) {
     connection.rollback();
     //handle error
     }
});

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

These days, As I know it, mariaDB module was not support bulk insert on the node.js.

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

发布评论

评论列表(0)

  1. 暂无评论