3

出于某种原因,当我写入“文本”类型的列时,我的查询搞砸了。这是一个例子:

Describe messages;

Field         Type          Null  Key  Default  Extra
id            int(11)       NO    PRI  NULL     auto_increment
title         varchar(255)  YES        NULL 
body          text          YES        NULL 
to            text          YES        NULL 
content_type  varchar(255)  YES        NULL 
is_sms        tinyint(1)    YES        NULL 
user_id       int(11)       YES        NULL 
created_at    datetime      YES        NULL 
updated_at    datetime      YES        NULL

然后我尝试插入:

INSERT INTO messages (id,title,body,to) VALUES ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );

由于某种原因,这会导致一般的 MySQL 语法错误。如果我删除“to”列并且它是查询中的相应值,则查询工作正常。

有任何想法吗?

4

5 回答 5

9

'to' 是 MySQL 中的保留关键字。您需要重命名列。

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

但是,如果您引用保留字,则允许它们作为标识符。

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

于 2009-04-15T15:38:21.643 回答
4

试试这个

INSERT INTO messages (`id`,`title`,`body`,`to`) 
   VALUES ('1','Test Message','This is a test message. 
   This is a test message. This is a test message. This is a test message.', 
   'an email' );
于 2009-04-15T15:40:21.857 回答
3
INSERT
INTO     messages (id,title,body,`to`)
VALUES   ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );
于 2009-04-15T15:39:38.053 回答
1

我相信如果你用这样的反引号包围“to”:

INSERT INTO messages (id,title,body,`to`) VALUES ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );

它会起作用的——反正对我有用。

于 2009-04-15T15:42:36.220 回答
0

使用 MySQL 中未定义的变量;例如:不使用'to','not','join' ...

INSERT INTO messages (id,title,body,test) VALUES ('1','Test Message','这是一条测试消息。这是一条测试消息。这是一条测试消息。这是一条测试消息。', '一封电邮' );

于 2013-07-16T13:02:28.440 回答