数据库INSERT语句

时间:2014-07-02 09:45:29

标签: java mysql sql jdbc

每次执行查询时是否需要创建新语句?

此行不起作用(我已经与数据库建立了连接)

Statement.execute("INSERT INTO PS2 (uuid , team, kills , deaths , rank_lvl, rank_name, pts) "
                    + "VALUES"
                    + " (`" + name.toString() + "` , `none` , 0 , 0 , 1 , `Starter` , 0)");

我使用以下代码创建表:

Statement.execute("CREATE TABLE IF NOT EXISTS PS2"
                + "(uuid VARCHAR(45),"
                + "team VARCHAR(20),"
                + "kills INTEGER,"
                + "deaths INTEGER,"
                + "rank_lvl INTEGER,"
                + "rank_name VARCHAR(25),"
                + "pts INTEGER)");

执行代码后,数据库已创建。之后,在尝试插入行之后,不会创建该行!

2 个答案:

答案 0 :(得分:2)

您使用反引号`而不是引号'来包装您的值。反引号仅用于包装列名称。

Statement.execute("INSERT INTO PS2 (`uuid` , team, kills , deaths , rank_lvl, rank_name, pts) "
    + "VALUES"
    + " ('" + name.toString() + "' , 'none' , 0 , 0 , 1 , 'Starter' , 0)");

正如Caweren所提到的,你应该小心SQL reserved keywordsUUID可能是其中之一(例如,使用Oracle V9i数据库),所以你应该用反引号包装它。

答案 1 :(得分:0)

uuid是SQL的保留关键字,你应该用引号括起来:`uuid`