mysql - 插入多个值时 Node mysql 上的“ER_PARSE_ERROR”

标签 mysql sql node.js node-mysql

我正在尝试使用 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 查询似乎都可以正常工作,所以我目前已连接到数据库。 如果需要更多信息,我很乐意提供。我只是不知道在这里放更多。

最佳答案

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

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

编辑

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

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

关于mysql - 插入多个值时 Node mysql 上的“ER_PARSE_ERROR”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24287767/

相关文章:

javascript - 从node.js中的PostgreSQL查询返回查询结果状态

php - MySQL - 获取过期日期前 30 天的数据

sql - H2删除查询限制

mysql - 当有 3 个日期列时按日期排序

sql - 对于 A 列中的每个不同值,在一行中返回 B 列的所有不同值

javascript - 有没有办法将变量从 Javascript 中的不同模块传递给导出的生成器?

php - 如何在 while 循环中为每个表单分配电子邮件地址

c# - 在 C# 中创建数据对象来保存表中的值

mysql - 我需要通过 fs.readfile 获取图像文件,我想将二进制数据存储在 MySQL 表中。我该怎么做

node.js - 将 KrakenD 与本地 Nodejs 服务器结合使用