简体   繁体   English

在 mysql CREATE TABLE 查询中设置变量值? 爪哇

[英]setting variable value in mysql CREATE TABLE query ? Java

thank you for your comments for my previous post here感谢您的意见对我以前的帖子在这里

I have tried making changes to my query accordingly with a change in table name:我尝试通过更改表名来相应地更改我的查询:

String str = getEmail();

String query = " CREATE TABLE "+str+"(email varchar(255), post_text varchar(255), post_image longblob,  post_time timestamp, post_date date, likes varchar(255), like_time timestamp, like_date date) ";

pstmt = cn.prepareStatement(query);

int i = pstmt.executeUpdate();

Now here is an error in syntax:现在这里有一个语法错误:

MySQLSyntaxErrorException: near.." '**null**(email varchar(255).."

I don't know why null ??我不知道为什么null And what is the error here?这里的错误是什么?

I am very new to Mysql..so please excuse n help.. much appreciated..thnx我对Mysql很陌生..所以请原谅n帮助..非常感谢..thnx

Looks like getEmail();看起来像getEmail(); returns null .返回null So your table name is null and you get these error message.所以你的表名是空的,你会收到这些错误消息。 Check it before calling the create statement.在调用 create 语句之前检查它。

You should not use special character in a table name.不应在表名中使用特殊字符。 If you want to do this, you have to escape the tablename with backticks:如果你想这样做,你必须用反引号转义表名:

String query = " CREATE TABLE `"+str+"` (email varchar(255), post_text varchar(255), post_image longblob,  post_time timestamp, post_date date, likes varchar(255), like_time timestamp, like_date date) ";

Your str String is null .你的str String 是null Please give a value to it.请给它一个值。

String str = getEmail();
if(str != null)
{
   //do your stuff here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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