【问题标题】:'ER_PARSE_ERROR' on node mysql when inserting multiple values插入多个值时节点 mysql 上的“ER_PARSE_ERROR”
【发布时间】:2014-08-08 21:07:00
【问题描述】:

我正在尝试使用 node-mysql 将多个值放入数据库中。 我的源代码

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;
});

我得到的错误是

[ [ 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)

目前我所有的其他 sql 查询似乎都可以正常工作,所以我目前已连接到数据库。 如果需要更多信息,我很乐意提供。我只是不知道在这里放更多。

【问题讨论】:

  • 3 个字段,只有 2 个值....
  • @RobertRozas 不是真的,它只是显示语法错误的位置。尝试在每个值上调用 app.mysql.escape(),因为它没有正确转义,正如您在 '16, 看到的那样。
  • @BenFortune 如果错误足够短,'16, 将结束,但无论如何我添加了转义,但显示相同的错误。我认为单引号来自错误打印

标签: mysql sql node.js node-mysql


【解决方案1】:

我最终只是在查询中使用了 mysql.escape(val)

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

编辑

解决此问题的方法是将 torrentFiles 添加到 [] 中。现在是

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

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多