插入方法上的 Discord.js 和 mysql2 错误
Posted
技术标签:
【中文标题】插入方法上的 Discord.js 和 mysql2 错误【英文标题】:Discord.js and mysql2 error on INSERT method 【发布时间】:2021-02-18 17:16:03 【问题描述】:我目前正在使用 mysql 编写 Discord 机器人。然后我尝试用mysql2做一个投票系统,但控制台总是出错,我不知道我做错了什么。任何人都可以帮助我吗?我会尽我所能,但我所做的都是假的
(我使用的包:discord.js、mysql2、sequelize 代码:
/**
*
* vote System
*
*/
const timeout = new Set();
const ctime = 86400;
client.on('message', async message =>
connection.query(
`SELECT * from \'vote'`
)
var voteData =
data: [
"username:": message.author.username,
"guild_id:": message.guild.id,
"guild_name": message.guild.name,
"votes": "1"
],
;
if(message.content.includes(`$config.prefixvote`))
if(timeout.has(message.guild.id))
return message.channel.send(`Es wurde heute bereits gevotet!`)
let embed = new Discord.MessageEmbed()
.setAuthor(`$message.author.tag`, message.author.avatarURL(format: "webp", size: 128))
.setTitle(`Server & Bot Vote!`)
.setDescription(`Sie haben erfolgreich Gevotet!\n Es kann in 24 Stunden erneut gevotet werden!`)
.setTimestamp()
.setColor('GREEN')
.setFooter(message.author.tag, message.author.avatarURL(format: "webp", size: 128))
message.channel.send(embed)
message.delete(reason: 'Timeouted!')
let vote = vote;
let guildId = guildId;
let username = username;
const connection = mysql.createPool(
host: 'localhost',
user: 'root',
database: 'rpd-bot'
);
/*fs.writeFile('./votes.json', JSON.stringify(voteData, null, 1), function (err)
if(err) throw err;*/
console.log(colors.green('Datensatz hinzugefügt!'))
connection.query(`SELECT * FROM rpd-bot`)
connection.promise()
.execute("INSERT INTO `vote`(`guildId`, `date`, `username`) VALUES (552,21.10,'test')")
timeout.add(message.guild.id)
setTimeout(() =>
timeout.delete(message.guild.id)
, ctime * 1000);
if('errno: 1064')
console.log(error)
)
我的控制台出错:
(node:27128) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'vote' before initialization
(node:27128) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
events.js:292
throw er; // Unhandled 'error' event
^
Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''vote'' at line 1
[... lines matching original stack trace ...]
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''vote'' at line 1"
【问题讨论】:
错误很清楚:您的 SQL 语法有错误。 【参考方案1】:您使用了错误的引号:
connection.query(
'SELECT * from `vote`'
)
请参阅how to escape schema and column names properly 上的手册。
由于 javascript 模板字符串和 SQL 转义引号之间的冲突,最好对 JavaScript 字符串使用单引号。因此:
'SELECT "x", `y` FROM `z`'
这两种引用样式都很好,不会干扰整个字符串。
【讨论】:
以上是关于插入方法上的 Discord.js 和 mysql2 错误的主要内容,如果未能解决你的问题,请参考以下文章
Node.js、discord.js 和 MySQL - ER_NO_SUCH_TABLE:表 'NAME' 不存在
Discord.js 和 mysql 检查新的数据库条目并输出它们?