javascript - Node JS MySQL 插入多变量查询 ER_PARSE_ERROR

标签 javascript mysql node.js string server

我创建了一个 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)

最佳答案

我猜问题出在这段代码上:

VALUES ("+timestamp.toString()+"

你能尝试转义那些双引号字符吗?

关于javascript - Node JS MySQL 插入多变量查询 ER_PARSE_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751902/

相关文章:

php - 仅当两列(持续时间和分数)不同时,如何增加 mysql 中的排名?

Java,MySQL - 线程异常 "main"java.sql.SQLException : Operation not allowed after ResultSet closed

node.js - node-mongodb-native MongoClient 意外关闭连接

mysql - 如何从通过中间表链接的 2 个父表获取数据

javascript - 使用 fabric.js 在按钮单击时在 Canvas 上添加图像

javascript - Mozilla 和 IE 修改了我的下拉列表的外观,但 chrome 没有

javascript - 顺序图节点 d3js

javascript - Scriptaculous Ajax.Autocomplete 加载栏

MySQL - 匹配所有标签而不是任何标签

javascript - 如何在nodejs程序中声明sequelize-auto?