Basically when insert into table SET ?
is used, the mysql npm package is scapping the quotes and that throws a parser error.
Code
var rows = [ { name: '100',
value: '100',
description: '100',
type: 'P',
scope: 'L' },
{ name: '101',
value: '101',
description: '101',
type: 'P',
scope: 'L' } ];
var sql = `INSERT into mytable SET ?;INSERT into mytable SET ?;`
connection.query(sql, rows, function(bulkInsertErr, bulkResult) {
....
});
Error Log
{ Error: ER_PARSE_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
'INSERT into variable SET `name` = '101', `value` = '101', `description` = '101',' at line 1
at Query.Sequence._packetToError (/foo/bar/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/foo/bar/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
at Protocol._parsePacket (/foo/bar/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/foo/bar/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/foo/bar/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/foo/bar/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/foo/bar/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/foo/bar/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
--------------------
at Protocol._enqueue (/foo/bar/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/foo/bar/node_modules/mysql/lib/Connection.js:198:25)
at /foo/bar/database/repository/VariableRepository.js:124:18
at DatabaseConnection.getConnection (/foo/bar/database/DatabaseConnection.js:65:9)
at VariableRepository.bulkInsert (/foo/bar/database/repository/VariableRepository.js:123:24)
at /foo/bar/routes/ApplicationVariableRouter.js:261:28
at Query.<anonymous> (/foo/bar/database/repository/ApplicationVariableRepository.js:224:11)
at Query.<anonymous> (/foo/bar/node_modules/mysql/lib/Connection.js:526:10)
at Query._callback (/foo/bar/node_modules/mysql/lib/Connection.js:488:16)
at Query.Sequence.end (/foo/bar/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage:
'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 \'INSERT into variable SET `name` = \'101\', `value` = \'101\', `description` = \'101\',\' at line 1',
sqlState: '42000',
index: 0,
sql:
'INSERT into variable SET `name` = \'100\', `value` = \'100\', `description` = \'100\', `type` = \'P\', `scope` = \'L\';INSERT into variable SET `name` = \'101\', `value` = \'101\', `description` = \'101\', `type` = \'P\', `scope` = \'L\';' }
As you can see, the generated sql string is:
INSERT into variable SET `name` = \'100\', `value` = \'100\', `description` = \'100\', `type` = \'P\', `scope` = \'L\';
INSERT into variable SET `name` = \'101\', `value` = \'101\', `description` = \'101\', `type` = \'P\', `scope` = \'L\';
If I paste this to my mysql ide, throws the same error obtained in nodejs:
But if I fix replacing \'
by '
in the ide, the rows are inserted.
I'm using ubuntu and this npm package "mysql": "2.18.1"
So, how can I fix that?