2013-02-28 159 views
1

以下是我的SQL查詢:SQL查詢中插入數據不能正常工作

嘗試1

INSERT INTO `product`(`productid`, `title`, `category`, `description`) 
VALUES (NULL,`iMac`,`Desktop`,`With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.`) 

嘗試2

INSERT INTO `product`(`productid`, `title`, `category`, `description`) 
VALUES(` `,`iMac`,`Desktop`,`With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.`) 

我不斷收到一個錯誤:MySQL returned an empty result set (i.e. zero rows). (Query took 0.0003 sec)

我不明白我做錯了什麼。這是我的列名列表

1 productid int(11)  
2 title varchar(100) 
3 category varchar(100) 
4 description varchar(2000) 

表名:產品

+0

你的報價是部分不正確。請參閱http://stackoverflow.com/questions/11321491/mysql-when-to-use-single-quotes-double-quotes-and-backticks字符串文字需要使用單引號,而不是反引號。 – 2013-02-28 14:10:42

+0

我也試過。它似乎沒有工作。 – 2013-02-28 14:17:39

+0

確實插入操作返回空結果集(即零行)?我很困惑... – 2013-02-28 14:33:16

回答

4

對於值,使用'字符,而不是這一個:```(對不起,我得找出如何放一個反引號成降價行內代碼塊...)

INSERT INTO `product`(`productid`, `title`, `category`, `description`) 
VALUES (NULL,'iMac','Desktop','With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.') 

這應該工作... Here is the SQLfiddle for it

編輯 這是解決按代爾鋁-Zour表定義:

INSERT INTO `product`(`title`, `category`, `description`) 
VALUES ('iMac','Desktop','With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.') 

你有兩件事情你忘了提: * productidPRIMARY KEY(因此,自動NOT NULL)列 - 任何在該列中使用NULL的插入都將失敗 * productid是一個AUTO_INCREMENT列 - 您甚至不必將其包含在INSERT語句中,每當您將其填入唯一值時插入一行

The SQL fiddle for this

+0

我相信內聯反向模式是反向,反斜槓,反向,反向...''''''''''' – 2013-02-28 14:16:09

+0

@MichaelBerkowski非常感謝,我正要試試這個古老的逃跑。編輯:它在一個評論,但不工作在一個答案?(我得到3反引號...) – ppeterka 2013-02-28 14:17:02

+0

嗯,它在我的評論中工作,但不是當我試圖編輯你的答案..這可能是一個錯誤。我要檢查元。 – 2013-02-28 14:17:07

0

你必須使用'varchar,數據類型,所以你的情況:

INSERT INTO product(productid, title, category, description) VALUES 
(
    NULL, 
    'iMac', 
    'Desktop', 
    'With its enhanced, big and beautiful display, the new Apple iMac M-D093-B/A 21.5 Desktop Computer renders your movies, photos, web pages and other graphics in truly jaw-dropping detail.' 
); 
+0

感謝您的快速響應但是這個查詢也得到了同樣的錯誤:(:(MySQL返回了一個空的結果集(即零行)(查詢花了0.0004秒) – 2013-02-28 14:17:03

+0

是productid您的主鍵如果是的話,這肯定不是NULL – puchmu 2013-02-28 14:20:39

+0

是的,我想因爲我已經添加爲自動增量,它會自己上傳一個數字,不是嗎? – 2013-02-28 14:25:57