SQL“CREATE TABLE”查询在java中不起作用

时间:2014-11-01 14:55:37

标签: java mysql sql

我在以下查询中收到错误,我找不到如何修复,这是查询代码:

CREATE TABLE IF NOT EXISTS `minecraftitems.blocks` (`player` varchar(16) NOT NULL, `itemid` text NOT NULL, `action` text NOT NULL,`time` big(20) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

错误代码:

10:48:05 [GRAVE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `blocks` (`player` varchar(16) NOT NULL, `itemid` tex' at line 1

编辑: 将查询更改为:

            PreparedStatement sql = con.prepareStatement(
                  "SELECT '" + db + "'; CREATE TABLE IF NOT EXISTS `minecraftitems.blocks` (\r\n" + 
                        "    `player` varchar(16) NOT NULL,\r\n" + 
                        "    `itemid` text NOT NULL,\r\n" + 
                        "    `action` text NOT NULL,\r\n" + 
                        "    `time` bigint(20) NOT NULL\r\n" + 
                        "    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;");

我现在收到此错误:

11:07:22 [GRAVE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `minecraftitems.blocks` (`player` varchar(16) N' at line 1

PhpMyAdmin运行查询没有任何问题。

3 个答案:

答案 0 :(得分:1)

您的查询中存在语法错误。其bigint不仅big将其更改为time bigint(20) NOT NULL

答案 1 :(得分:1)

这是您的查询:

CREATE TABLE IF NOT EXISTS `minecraftitems.blocks` (
    `player` varchar(16) NOT NULL,
    `itemid` text NOT NULL,
    `action` text NOT NULL,
    `time` big(20) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

问题是big不是数据类型。您的意思是biginttimetimestamp还是别的?将名为time的字段存储为整数似乎很可疑。

注意:这会创建一个名为" mincraftitems.blocks"的表,这似乎是该语句的意图。如果您尝试在blocks中创建minecraftitems,那么您需要:

CREATE TABLE IF NOT EXISTS `minecraftitems`.`blocks` (

答案 2 :(得分:0)

在一行中制作,并删除了SELECT db;它起作用了。这是我的查询

PreparedStatement sql = con.prepareStatement("CREATE TABLE IF NOT EXISTS " + db + ".blocks (`player` varchar(16) NOT NULL, `itemid` text NOT NULL, `location` text NOT NULL, `action` text NOT NULL, `time` bigint(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1");