简体   繁体   中英

mySQL syntax error when using an integer

I'm trying to create a table using mySQL and Java. What I have is:

String sql = "CREATE TABLE IF NOT EXISTS " + Userinput.getTableName2() +
" (participant INT(255), " +
" 0 INT(255),"+ 
" name INT(3), " + 
" occurances INT(255))";
stmt.executeUpdate(sql);

The Zero naming the second column is arbitrary, but I will need to have the column name be an integer. The error I'm getting is:

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 '0 INT(255), name INT(3), occurances INT(255))' at line 1

I would appreciate any and all help! Thanks!

According to this http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

so you have to quote the 0

String sql = "CREATE TABLE IF NOT EXISTS " + Userinput.getTableName2() +
" (participant INT(255), " +
" `0` INT(255),"+                     // using backticks
" name INT(3), " + 
" occurances INT(255))";
stmt.executeUpdate(sql);

Although, it seems a dumb name for a column.

Please read mysql documentation:

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM