无法将字符串插入 MySQL 文本列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/752277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot insert string into MySQL text column
提问by Tony
For some reason, my queries screw up when I write to a column of type "text". Here is an example:
出于某种原因,当我写入“文本”类型的列时,我的查询搞砸了。下面是一个例子:
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
Then I try an insert:
然后我尝试插入:
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' );
For some reason this causes a general MySQL syntax error. The query works fine if I remove the "to" column and it's corresponding value from the query.
出于某种原因,这会导致一般的 MySQL 语法错误。如果我从查询中删除“to”列及其对应的值,则查询工作正常。
Any ideas?
有任何想法吗?
回答by
'to' is a reserved keyword in MySQL. You'll need to rename your column.
'to' 是 MySQL 中的保留关键字。您需要重命名列。
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
However, Reserved words are permitted as identifiers if you quote them.
但是,如果引用保留字,则允许将其用作标识符。
回答by ólafur Waage
Try this instead
试试这个
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' );
回答by Quassnoi
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' );
回答by itsmatt
I believe if you surround the "to" with backtics like so:
我相信如果你像这样用反引号包围“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' );
it will work - did for me anyway.
它会起作用 - 反正对我有用。
回答by huseyin
Use a variable is not defined in MySQL; for example : not use 'to', 'not', 'join' ...
使用 MySQL 中未定义的变量;例如:不使用“to”、“not”、“join”...
INSERT INTO messages (id,title,body,test) 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' );
INSERT INTO messages (id,title,body,test) VALUES ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', '一封电邮' );