question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on bulk insert with execute()

See original GitHub issue

When I try to insert multiple rows with a bulk insert and use the execute() method I get the error:

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

It works, when I use the query() method.

This is the interesting part of my code:

const con = await getConnection()
await con.beginTransaction()

const values = [name, comments, levelId, constructionId, id]
const sql = "UPDATE structures SET name = ?, comments = ?, level = ?, construction_id = ? WHERE structure_id = ?;"
await con.execute(sql, values)

const sql2 = "INSERT IGNORE INTO structure_categories (structure_id, category_id) VALUES ?;"
const values2 = categoryIds.map(categoryId => {
    return [id, categoryId]
})
await con.execute(sql2, [values2])

await con.commit()
await con.end()

Seems like the replacement of the ? on bulk inserts doesn’t work properly with the execute() method.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
sidorarescommented, Nov 9, 2020

your query ( including “?” symbols ) must be a valid input for PREPARE command, which is likely isn’t

also number of placeholders should be exactly the same as number of parameters. Few options here:

  • just use .query() - that would interpolate parameters on the driver side and send long sql query with all the data in it
  • dynamically insert a bunch of placeholders ( new Array(10).fill('?').join(',') ) to match the number of parameters. That way you’ll have statement prepared each time number is different but reused for same number of parameters, and you also benefit from separate processing of query and data which is potentially safer from sql injection point of view
  • create a stored procedure that accepts array as JSON object and can iterate all elements in it and insert data server side: conn.execute('CALL sp_bulk_insert(?)', [JSON.stringify(data)])
0reactions
sidorarescommented, Apr 11, 2022

also LOAD DATA INFILE might have slightly better performance as you send your data as csv. You can pass the content of the file with the query, doesn’t have to be “real” file - see https://github.com/sidorares/node-mysql2/blob/master/documentation/Extras.md#sending-tabular-data-with-load-infile-and-local-stream

https://stackoverflow.com/questions/51673896/load-data-infile-and-spatial-data might be helpful

Read more comments on GitHub >

github_iconTop Results From Across the Web

An error message may occur when you run a "BULK INSERT ...
Fixes an error message that may occur when you run a "BULK INSERT" query on a database that uses the "BULK_LOGGED" or "SIMPLE"...
Read more >
Working With Line Numbers and Errors Using Bulk Insert
In this blog post, we look at these techniques using T-SQL's native bulk insert (Line Numbers and Errors Using Bulk Insert).
Read more >
MongoDB: Handle errors from Bulk.insert() method
I have no problems with data insertion, but I want to get the answer, because there are duplicate records and I need to...
Read more >
Troubleshooting Common SQL Server Bulk Insert Errors
The column size must be able to hold the data size of the values being inserted. The error identifies the specific problem -...
Read more >
Bulk insert fails, but no error thrown - Google Groups
res = cursor.execute(sql). If I run this on SQL server studio I get the following: Msg 4864, Level 16, State 1, Line 1....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found