'ER_PARSE_ERROR' on node mysql when inserting multiple values

buhbang picture buhbang · Jun 18, 2014 · Viewed 28.6k times · Source

I'm trying to put in multiple values into database using node-mysql in node. my source code

engine.files.forEach(function(file) {
  torrentpath = file.path.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;").replace(/\\/g, "/");
  torrentFiles.push([
    result.insertId,
    torrentpath,
    file.length
  ]);
});

console.log(torrentFiles);

app.mysql.query('INSERT INTO `files` (`torrentid`, `filename`, `filesize`) VALUES ?', torrentFiles, function(err, result) {
  if (err) throw err;
});

The error I get is

[ [ 11, 'ubuntu-14.04-desktop-amd64.iso', 1010827264 ] ]

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 '11, 'ubuntu-14.04-desktop-amd64.iso', 1010827264' at line 1
    at Query.Sequence._packetToError (C:\wamp\www\sleepytorrentdownload\src\node_modules\mysql\lib\protocol\sequences\Sequence.js:30:14)

Currently all of my other sql queries seem to work so I am currently connected to database. If more info is needed, I'll gladly give them. I just dont know more to put on here.

Answer

buhbang picture buhbang · Jun 18, 2014

I ended up just using mysql.escape(val) inside the query

 app.mysql.query('INSERT INTO `files` (`torrentid`, `filename`, `filesize`) VALUES '+app.mysql.escape(torrentFiles), function(err, result) 

EDIT

The fix for this was to add put torrentFiles in []. it is now

app.mysql.query('INSERT INTO `files` (`torrentid`, `filename`, `filesize`) VALUES ? ', [torrentFiles], function(err, result)