gpt4 book ai didi

java - 使用整数时出现 mySQL 语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:13 26 4
gpt4 key购买 nike

我正在尝试使用 mySQL 和 Java 创建一个表。我拥有的是:

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

第二列的零命名是任意的,但我需要让列名是一个整数。我得到的错误是:

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

我将不胜感激任何帮助!谢谢!

最佳答案

根据这个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.

所以你必须引用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);

虽然,对于列来说,这似乎是一个愚蠢的名字。

关于java - 使用整数时出现 mySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22029583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com