1

每当我尝试运行我的代码时,这都会显示列数不匹配错误。

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 }
4

2 回答 2

7

如果有人仍然想知道这一点,您可以使用 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

于 2019-02-12T11:27:39.623 回答
-2

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

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

于 2018-11-08T03:09:13.153 回答