简体   繁体   English

如何使用 mysljs 在 mySql 和 node.js 中批量插入

[英]How to bulk insert in mySql and node.js using mysljs

Im not able to use bulk insert in my DB using node.js lib mysljs .我无法使用 node.js lib mysljs在我的数据库中使用批量插入。

I followed answers from:我遵循以下答案:

How do I do a bulk insert in mySQL using node.js 如何使用 node.js 在 mySQL 中进行批量插入

with no success.没有成功。

var sql = "INSERT INTO resources (resource_container_id, name, title, extension, mime_type, size) VALUES ?";

var values = [
  [1, 'pic1', 'title1', '.png', 'image/png', 500], 
  [1, 'pic2', 'title2', '.png', 'image/png', 700]];

return connection.query(sql, [values], (result) => {
    if (err) throw err;
    connection.end();
});

I keep getting error:我不断收到错误:

 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \'?\' at line 1' 

I also tried to promisify the query mehod using bluebird but with no success, I get same error again.我还尝试使用 bluebird 来保证查询方法,但没有成功,我再次遇到相同的错误。

You need to mark your keys with the grave accent (backtick) character, like this: `key`你需要用重音符(反引号)来标记你的键,像这样:`key`

Making your query like this:像这样进行query

var sql = "INSERT INTO resources (`resource_container_id`, `name`, `title`, `extension`, `mime_type`, `size`) VALUES ?";

尝试在问号周围使用 ()

var sql = "INSERT INTO resources (resource_container_id, name, title, extension, mime_type, size) VALUES (?) ";

I have the same issue only when i use connection.execute() and then it's actually because it is not implemented for prepared statements.我只有在使用connection.execute()时才会遇到同样的问题,然后实际上是因为它没有为准备好的语句实现。

I resolved the issue by using connection.query() instead.我通过使用connection.query()解决了这个问题。

Don't know if that answer helps anyone tho.不知道这个答案是否对任何人有帮助。

It would help me when I was searching for the answer.当我寻找答案时,它会帮助我。

尝试删除values周围的方括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM