如何将MySQL连接到Discord.js?

8yparm6h  于 2022-12-28  发布在  Mysql
关注(0)|答案(1)|浏览(94)

在这段代码中,我尝试将MySQL连接到Discord.js,但是当我运行命令时,我遇到了这样的结果。我该如何解决这个问题,或者我可以用不同的方法将MySQL连接到Discord.js?

const { SlashCommandBuilder } = require("discord.js");
const mysql = require("mysql");
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "25m"
});

connection.connect(function(err) {
    if(err) throw err;
   
});
  
module.exports = {
    data: new SlashCommandBuilder()
    .setName("test")
    .setDescription("test"),
  
    async execute (client, interaction) {
        
        connection.query(`SELECT * FROM cypter WHERE id = '2'`,function(result) {
                interaction.reply({content: `Result:${result}`})
        });
    }
}

s3fp2yjn

s3fp2yjn1#

我也在使用mysql包。我是这样做的:

var pool = mysql.createPool({
    host: "localhost",
    user: "root",
    password: "",
    database: "25m"
    port: //Enter port,
});

var connection = pool.getConnection(function (err, con) {
    if (err) {
        console.log("Error DB connect: " + err);
        throw err;
    }
    else {
        connection = con;
        console.log("Connection established.");
    }

});

对于我的查询,我需要使用'来查找表和列名,如(我从2中删除了',因为我认为它是一个数字,如果不是,您可以再次使用它):

connection.query("SELECT * FROM `cypter` WHERE `id` = '2';",function(result){
    interaction.reply({content: `Result:${result}`})
})

相关问题