Error on bulk insert with execute()
See original GitHub issueWhen 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:
- Created 3 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
.query()
- that would interpolate parameters on the driver side and send long sql query with all the data in itnew 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 viewconn.execute('CALL sp_bulk_insert(?)', [JSON.stringify(data)])
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-streamhttps://stackoverflow.com/questions/51673896/load-data-infile-and-spatial-data might be helpful