2013-02-28 158 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