Mysql错误:截断错误的DOUBLE值:' world'

时间:2014-07-07 01:00:55

标签: java mysql

我已经尝试了最后40分钟来找出这个错误。错误Truncated incorrect DOUBLE value显然意味着:comparing a string column with an integer, because they both get converted to double for the comparison

这是我的表:

this.sql.query("CREATE TABLE IF NOT EXISTS `back_locations` ("
                    + "`playerID` INT(11) NOT NULL, "
                    + "`world` VARCHAR(255) NOT NULL, "
                    + "`x` INT(11) DEFAULT 0, "
                    + "`y` INT(11) DEFAULT 0, "
                    + "`z` INT(11) DEFAULT 0 )");

这是带错误的方法(带有UPDATE查询的行):

public void DB_setBackLocation( Location loc, int playerID )
{
    try 
    {
        String world = loc.getWorld().getName();
        int x = (int)(loc.getBlockX());
        int y = (int)(loc.getBlockY());
        int z = (int)(loc.getBlockZ());

        ResultSet rs = sql.query( "SELECT * FROM back_locations WHERE playerID = " + playerID + "" );

        if( rs != null && rs.next() )
        {
            //The next line is the line with an error
            sql.query( "UPDATE back_locations SET world=\"" + world + "\" AND x=" + x + " AND y=" + y + " AND z=" + z + " WHERE playerID=" + playerID + "" );
        }
        else
        {
            sql.insert("back_locations", 
                    new Object[]{ "playerID", "world", "x", "y", "z"}, 
                    new Object[]{ playerID, world, x, y, z});
        }
    } 
    catch (SQLException e) 
    {
        e.printStackTrace();
    }
}

我的错误: The error

感谢您提前回答。

祝贺亚历山大

编辑1:

这是打印的行

debug message

2 个答案:

答案 0 :(得分:2)

随后的AND会导致您的字符串被评估为布尔值,这会导致您看到的警告。要解决此问题,只需使用逗号替换AND即可更正语法。

答案 1 :(得分:1)

您的update语法错误,

"UPDATE back_locations SET world=\"" + world + "\" AND x=" 
    + x + " AND y=" + y + " AND z=" + z + " WHERE playerID=" + playerID + ""

看起来应该是,

"UPDATE back_locations SET world='" + world + "', x=" 
    + x + ", y=" + y + ", z=" + z + " WHERE playerID=" + playerID

我真的建议您使用带有PreparedStatement的绑定参数。