首页 > 解决方案 > SQL WHERE 不适用于 Discord

问题描述

所以我得到一个数据库,我的不和谐机器人正在连接到数据库,但是当我得到它时,我得到一个错误 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'd1aa852e1a504d6cafbd55e0d2d28dea' in 'where clause' 这是我的代码

int id = SQLUser.getIntFromDatabase("SELECT * FROM Players WHERE UUID = " + uuid.toString() + "", "ID");
if(id==-1) channel.sendMessage("ID is not valid please login to the WidowMC").queue();
else 
{
    Message messa = new MessageBuilder().append(Utils.getName(uuid) + " Skull").build();
    channel.sendFile(Utils.getSkullFile(Utils.getName(uuid)), messa).queue();
    channel.sendMessage("ID = " + id);
}

public static void excuteQuery(String query) 
{
    Connection conn = Core.getConnection();
    try
    {
        Statement statement = conn.createStatement();
        statement.executeQuery(query);
    } catch (SQLException e)
    {
        e.printStackTrace();
    }
}

标签: javadiscord

解决方案


您需要在 uuid 周围添加引号以使其成为文字,否则 Mysql 认为您指的是列。

另外要非常小心,您的代码容易受到 SQL 注入的影响,正确使用Prepared Stamements可以帮助您消除该漏洞。


推荐阅读