Search code examples
c#asp.netmysqlprimary-keycreate-table

MySQL - CREATE TABLE trows error when similar CREATE TABLE doesn't


I am making an ASP.NET website at the moment. In the Global.asax I have code for initializing some tables if they does not exist. The init blog_post and booked_dates part of the code executes properly and creates the table but the init image_info does not. They are quite similar and I cannot find what is wrong with the MySQL syntax.

Error message:
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 ''description' TEXT, PRIMARY KEY(id))' at line 1

        //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();
        }

Thanks for the help beforehand!


Solution

  • You are using different set of quotes around description ' it should be the other one

    `description`
    

    or since description is not a reserve word, you can omit the quotes if you want. You query should be:

    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))