0

我现在正在制作一个 ASP.NET 网站。在 Global.asax 中,如果某些表不存在,我有代码用于初始化它们。代码的 init blog_post 和 booking_dates 部分正确执行并创建表,但 init image_info 没有。它们非常相似,我找不到 MySQL 语法有什么问题。

错误信息:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''description' TEXT, PRIMARY KEY(id))' 附近使用正确的语法

        //init blog_post
        using (MySqlCommand cmd = new MySqlCommand("CREATE TABLE IF NOT EXISTS `blog_post` (" +
            "`id` INT AUTO_INCREMENT," +
            "`header` VARCHAR(255)," +
            "`bbcode` TEXT," +
            "`author` VARCHAR(255)," +
            "`time_posted` DATETIME, " +
            "PRIMARY KEY(id));", con))
        {
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

        //init booked_dates
        using (MySqlCommand cmd = new MySqlCommand("CREATE TABLE IF NOT EXISTS `booked_dates` (" +
            "`id` INT AUTO_INCREMENT," +
            "`author` VARCHAR(255)," +
            "`start_time` DATETIME," +
            "`end_time` DATETIME," +
            "`time_booked` DATETIME, " +
            "PRIMARY KEY(id));", con))
        {
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

        //init image_info
        using (MySqlCommand cmd = new MySqlCommand("CREATE TABLE IF NOT EXISTS `image_info` (" +
            "`id` INT AUTO_INCREMENT," +
            "`author` VARCHAR(255)," +
            "`file_name` TEXT," +
            "`title` TEXT," +
            "`album` TEXT, " +
            "'description' TEXT, " +
            "PRIMARY KEY(id));", con))
        {
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

提前感谢您的帮助!

4

5 回答 5

1

您没有backticks在创建表代码中使用。

"'description' TEXT, "

应该

"`description` TEXT, "
于 2013-03-15T12:07:57.180 回答
0

您在描述周围使用了不同的引号,'它应该是另一个

`description`

或者由于description不是保留词,因此您可以根据需要省略引号。您的查询应该是:

using (MySqlCommand cmd = new MySqlCommand("CREATE TABLE IF NOT EXISTS `image_info` (" +
            "`id` INT AUTO_INCREMENT," +
            "`author` VARCHAR(255)," +
            "`file_name` TEXT," +
            "`title` TEXT," +
            "`album` TEXT, " +
            "`description` TEXT, " +
            "PRIMARY KEY(id));", con))
于 2013-03-15T12:06:58.930 回答
0

backticks应该被用来代替single quote

"'description' TEXT, " +

本来应该

"`description` TEXT, " +
于 2013-03-15T12:07:06.713 回答
0

"'description' TEXT, " +有其他报价!

于 2013-03-15T12:07:37.493 回答
0

无论如何都不需要引用/反引号。

于 2014-07-25T07:26:03.303 回答