2014-03-28 100 views
0

MySQL查詢mysql語法錯誤爲什麼?

CREATE TABLE IF NOT EXISTS 'sadakin'.'gcm_users' (

    'id' int(11) NOT NULL AUTO_INCREMENT , 
    'gcm_regid' text, 
    'name' varchar(50) NOT NULL , 
    'email' varchar(255) NOT NULL , 
    'imei' varchar(20) NOT NULL , 
    'created_at' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , 
    PRIMARY KEY ('id') , 
    KEY 'imei' ('imei') 
    ) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT =1; 

錯誤消息

1064 - 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 ''sadakin'.'gcm_users' (

     'id' int(11) NOT NULL AUTO_INCREMENT , 
      'gcm_' at line 1 

我沒有找到錯誤。幫我!

回答

4

使用反引號來標識標識符。行情只是字符串:

CREATE TABLE IF NOT EXISTS `sadakin`.`gcm_users` (

     `id` int(11) NOT NULL AUTO_INCREMENT , 
     `gcm_regid` text, 
     `name` varchar(50) NOT NULL , 
     `email` varchar(255) NOT NULL , 
     `imei` varchar(20) NOT NULL , 
     `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , 
     PRIMARY KEY (`id`) , 
     KEY `imei` (`imei`) 
     ) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT =1; 
+0

基本錯誤。謝謝 – sadakin

0

有列名無需任何單引號的

CREATE TABLE IF NOT EXISTS sadakin.gcm_users (

     id int(11) NOT NULL AUTO_INCREMENT , 
     gcm_regid text, 
     name varchar(50) NOT NULL , 
     email varchar(255) NOT NULL , 
     imei varchar(20) NOT NULL , 
     created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , 
     PRIMARY KEY (id) , 
     KEY imei (imei) 
     ) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT =1; 
0

你必須使用反單引號,而不是單引號。引號用於字符串文字。

檢查:http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

字符串是字節或字符序列,無論是 單引號(「'」)內封閉的或雙引號(「「」)

嘗試:

CREATE TABLE IF NOT EXISTS `sadakin`.`gcm_users` (

    `id` INT(11) NOT NULL AUTO_INCREMENT , 
    `gcm_regid` TEXT, 
    `name` VARCHAR(50) NOT NULL , 
    `email` VARCHAR(255) NOT NULL , 
    `imei` VARCHAR(20) NOT NULL , 
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , 
    PRIMARY KEY (`id`) , 
    KEY `imei` (`imei`) 
    ) ENGINE = INNODB DEFAULT CHARSET = latin1 AUTO_INCREMENT =1;