14

Im not able to use bulk insert in my DB using node.js lib mysljs.

I followed answers from:

How do I do a bulk insert in mySQL using node.js

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.

1
  • typo in the callback there should be (err, result)=>{...} but that is likely not the error you describe here, since the err is actualy printed at some point so i assume that it's just a copy error. Commented Jun 3, 2021 at 22:34

4 Answers 4

1

You need to mark your keys with the grave accent (backtick) character, like this: `key`

Making your query like this:

var sql = "INSERT INTO resources (`resource_container_id`, `name`, `title`, `extension`, `mime_type`, `size`) VALUES ?";
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work for me, still get the error.
1

try using () around the question mark

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

1 Comment

I have the same issue with mysql2 library and also doesn't work.
1

I have the same issue only when i use connection.execute() and then it's actually because it is not implemented for prepared statements.

I resolved the issue by using connection.query() instead.

Don't know if that answer helps anyone tho.

It would help me when I was searching for the answer.

Comments

-2

Try removing the square brackets around values

1 Comment

square brackets around values imply array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.