MySQL - 当类似的CREATE TABLE没有时,CREATE TABLE抛出错误

时间:2013-03-15 12:04:15

标签: c# asp.net mysql primary-key create-table

我目前正在制作一个ASP.NET网站。在Global.asax中,我有一些代码用于初始化某些表(如果它们不存在)。 init blog_post和booked_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();
        }

事先感谢您的帮助!

5 个答案:

答案 0 :(得分:1)

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

"'description' TEXT, "

应该是

"`description` TEXT, "

答案 1 :(得分: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))

答案 2 :(得分:0)

应该使用

backticks代替single quote

"'description' TEXT, " +

应该是

"`description` TEXT, " +

答案 3 :(得分:0)

"'description' TEXT, " +还有其他引用!

答案 4 :(得分:0)

无论如何都不需要引用/反击它们。