Discord.js 和 mysql2 INSERT 方法错误

Discord.js and mysql2 error on INSERT method

我目前正在使用 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.prefix}vote`)) {
    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"
}

您使用了错误的引号:

connection.query(
  'SELECT * from `vote`'
)

参见 how to escape schema and column names properly 上的手册。

由于 JavaScript 模板字符串和 SQL 转义引号之间的冲突,最好对 JavaScript 字符串使用单引号。因此:

'SELECT "x", `y` FROM `z`'

这里两种引号样式都很好,不会干扰整个字符串。