javascript - 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)
解决方案
我会做这样的事情。
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}" ...)`;
推荐阅读
- spring - 看不到 spring mvc 验证错误消息
- excel-2007 - 在一个excel文件中,我想从左边重写一个字符串来写
- python - 创建用于创建 .zip 文件的脚本文件的最佳方法是什么?
- react-hooks - 使用 useRef 动态关注输入(子组件)不起作用
- android - 使用最新 API 30 (Android 10) 构建的 Android 应用程序仅在 Android 10 中运行,在 Android 8 (OREO) 中运行时应用程序崩溃
- ios - 如何设置文本字段字符限制 SwiftUI?
- node.js - 猫鼬模式,无效的模式配置
- php - 如何从 php 页面上的特定操作中获取数据
- python - 我无法在 Django 中从我的应用程序中删除用户
- gradle - Vaadin 14 生产模式和 Safari 问题