mysql erparseerror,插入多个值时,节点mysql上出现“ ER_PARSE_ERROR”

本文介绍了一位开发者使用Node.js和node-mysql模块在MySQL数据库中批量插入数据时遇到的问题及解决办法。主要错误在于SQL语法不当,通过将值数组正确包装并使用mysql.escape()方法确保了SQL语句的正确性。
摘要由CSDN通过智能技术生成

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, "&").replace(/>/g, ">").replace(/

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.

解决方案

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值