2013-03-15 122 views
0

我正在製作一個ASP.NET網站。在Global.asax中,我有用於初始化某些表的代碼(如果它們不存在)。 init的blog_post和booked_dates部分代碼正確執行並創建表,但是init image_info沒有。它們非常相似,我無法找到MySQL語法有什麼問題。MySQL - CREATE TABLE在類似CREATE TABLE時出錯錯誤

錯誤消息:
您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的'說明「TEXT,PRIMARY KEY(ID))」在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(); 
     } 

感謝事先幫助正確的語法手冊!

+0

在global.asax中創建表並不好主意 – Sergio 2013-03-15 12:08:27

回答

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

工程!謝謝您的幫助!我會在10分鐘內接受答案。 – Highace2 2013-03-15 12:08:47

+0

@ Highace2,歡迎您 – Habib 2013-03-15 12:09:09

0

backticks應該被用來代替single quote

"'description' TEXT, " + 

應該已經

"`description` TEXT, " + 
0

"'description' TEXT, " +有其他的報價!

1

您在創建表格代碼中沒有使用backticks

"'description' TEXT, " 

應該

"`description` TEXT, " 
0

無需報價/ BACKTICK他們無論如何。