插入多个值时,节点mysql上出现"ER_PARSE_ERROR" [英] 'ER_PARSE_ERROR' on node mysql when inserting multiple values

查看:711
本文介绍了插入多个值时,节点mysql上出现"ER_PARSE_ERROR"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我得到的错误是

[ [ 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查询似乎都可以正常工作,因此我目前已连接到数据库. 如果需要更多信息,我会很乐意提供给他们.我只是不知道要穿什么.

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.

推荐答案

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

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) 

编辑

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

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)

这篇关于插入多个值时,节点mysql上出现"ER_PARSE_ERROR"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆