'ER_PARSE_ERROR' in mysql node when inserting multiple values

I am trying to enter multiple values ​​into a 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 my other sql queries seem to work, so I am currently connected to the database. If you need more information, I will gladly give them. I just don't know anymore to do here.

+4
source share
1 answer

In the end, I just used 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 put put torrentFilesin []. Now

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

Source: https://habr.com/ru/post/1545081/


All Articles