2014-11-01 130 views
1

我得到以下查询,我无法找到如何解决错误不能正常工作,这里的查询代码:SQL“CREATE TABLE”查询在java中

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无任何问题地运行查询。

+2

这个问题似乎是题外话,因为它是一个“查找我的错别字问题” – bummi 2014-11-01 18:51:41

回答

1

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

+0

仍然得到错误 – ax752 2014-11-01 18:28:53

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”的表,这似乎是该语句的意图。如果您正在尝试创造minecraftitemsblocks,那么你会想:

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

难道不应该也是'\'minecraftitems \'\'块\ ''? (反引号每个数据库和表名) – 2014-11-01 15:01:14

+0

@a_horse_with_no_name。 。 。也许出于一些不合理的原因,OP想要一个表名的时期。我为此添加了一个注释。 – 2014-11-01 15:03:34

+0

它是一个时间戳,我想在名为blocks的数据库中创建一个表。 – ax752 2014-11-01 15:07:59

0

在制造在同一行,并取消了SELECT分贝;它的工作。这里是我的查询

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");