Akash Sourav Nayak
Akash Sourav Nayak

Reputation: 203

How to do bulk insert In mariadb using nodejs?

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

}); 

Error:

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

Upvotes: 2

Views: 3852

Answers (2)

jarrydrichards
jarrydrichards

Reputation: 109

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

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

Upvotes: 9

Yunho Jang
Yunho Jang

Reputation: 11

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

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

Upvotes: -1

Related Questions