首页 > 解决方案 > Node JS MySQL插入查询多变量ER_PARSE_ERROR

问题描述

我创建了一个 webhook 来使用 Node.JS 在此处接收一些数据。我正在使用 MySQL 存储我的数据,到目前为止,服务器能够连接到数据库,接收客户端数据,并在接收到数据时做出适当的响应。我只是在格式化 MySQL 的查询字符串时遇到问题。

我尝试了各种引号配置,例如不将列名包裹在单引号中,不包裹表名,以及在查询字符串中的每个部分在双引号和单引号之间切换。

任何建议,将不胜感激!谢谢!

我的查询:

var sql = "INSERT INTO 'cms'('ts', 'orgid', 'orgname', 'deviceid', 'devicename', 'event', 'setid', 'setname', 'viewid', 'viewname', 'duration', 'hotspotid', 'hotspotname', 'playlistid', 'playlistname', 'playlisttype', 'playlistassetid', 'playlisttitle') VALUES ("+timestamp+", "+orgid+", "+orgname+", "+deviceid+", "+devicename+", "+eventname+", "+setid+", "+setname+", "+viewid+", "+viewname+", "+duration+", "+hotspotid+", "+hotspotname+", "+playlistid+", "+playlistname+", "+playlisttype+", "+playlistassetid+", "+playlisttitle+")";
                            con.query(sql, function (err, result) { //Queries String with MySQL connection variable con
                                    if (err) throw err;
                                    var date = new Date();
                                    var currHour = date.getHours();
                                    console.log("Record Inserted at " + currHour); //Logs time of db insertion on console
                            });

这也是错误:

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 ''cms'('ts', 'orgid', 'orgname', 'deviceid', 'devicename', 'event', 'setid', 'set' at line 1
at Query.Sequence._packetToError (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/Protocol.js:278:23)
at Parser.write (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/webhook/httpHandler/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/home/webhook/httpHandler/node_modules/mysql/lib/Connection.js:502:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
--------------------
at Protocol._enqueue (/home/webhook/httpHandler/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/webhook/httpHandler/node_modules/mysql/lib/Connection.js:200:25)
at IncomingMessage.<anonymous> (/home/webhook/httpHandler/server.js:70:9)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)

标签: javascriptmysqlnode.jsstringserver

解决方案


我会做这样的事情。

var sql = `INSERT INTO 'cms'('ts', 'orgid', 'orgname', 'deviceid', 'devicename', 'event', 'setid', 'setname', 'viewid', 'viewname', 'duration', 'hotspotid', 'hotspotname', 'playlistid', 'playlistname', 'playlisttype', 'playlistassetid', 'playlisttitle') VALUES ("${timestamp}", "${orgid}" ...)`;

推荐阅读