MySQL没有插入反斜杠

时间:2012-10-07 16:34:47

标签: mysql sql

在MySQL中,当我尝试在我的表中插入一个反斜杠时,它不接受它并给我没有反斜杠的内容。

id设置为自动增量:

代码:

INSERT INTO gender (sex, date) VALUES (
'male are allowed \ female are not allowed', "2012-10-06")

如何插入文字反斜杠?

有关转义序列的说明:

Escape  Sequence    Character Represented by Sequence

\0     An ASCII NUL (0x00) character.
\'     A single quote (“'”) character.
\"     A double quote (“"”) character.
\b     A backspace character.
\n     A newline (linefeed) character.
\r     A carriage return character.
\t     A tab character.
\Z     ASCII 26 (Control+Z). See note following the table.
\\     A backslash (“\”) character.
\%     A “%” character. See note following the table.
\_     A “_” character. See note following the table.

3 个答案:

答案 0 :(得分:21)

你需要逃避反斜杠:

INSERT INTO gender
(sex, date) VALUES (
'male are allowed \\ female are not allowed',
"2012-10-06")

Reference (with the list of all characters you must escape for mysql)

答案 1 :(得分:5)

如何在mysql加载数据infile工具中驯服反斜杠:

第1步,创建表格

mysql> create table penguin (id int primary key, chucknorris VARCHAR(4000));
Query OK, 0 rows affected (0.01 sec)

第2步,创建要导入的文件并将此数据放入其中。

1   aliens are on route
2   scramble the nimitz\
3   \its species 8472
4   \\\\\\\\\\\\\\\\\\
5   Bonus characters:!@#$%^&*()_+=-[]\|}{;'":/.?>,< anything but tab

第3步,插入您的表格:

mysql> load data local infile '/home/el/foo/textfile.txt' into table penguin 
       fields terminated by '\t' lines terminated by '\n' 
       (@col1, @col2) set id=@col1, chucknorris=@col2;
Query OK, 4 rows affected, 1 warning (0.00 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 1

第4步,当然,它会导致这种奇怪的输出:

mysql> select * from penguin;
+----+-----------------------------------------------------------------+
| id | chucknorris                                                     |
+----+-----------------------------------------------------------------+
|  1 | aliens are on route                                             |
|  2 | scramble the nimitz                                             |
|  3 |                                                                 |
|  4 | \\\\\\\\\                                                       |
|  5 | Bonus characters:!@#$%^&*()_+=-[]|}{;'":/.?>,< anything but tab |
+----+-----------------------------------------------------------------+

步骤5,分析警告:

mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level   | Code | Message                                                |
+---------+------+------------------------------------- ------------------+
| Warning | 1262 | Row 2 was truncated; it contained more data than there |
|         |      | were input columns                                     |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)

第6步,考虑到底出了什么问题:

nimitz左侧的反斜杠导致mysql加载数据解析器将第2行的结尾与第3行的开头连接起来。然后它碰到了一个标签并将'nimitz \ n3'加入第2行。

第3行的其余部分被跳过,因为额外的单词its species 8472不适合任何地方,它会产生您在上面看到的警告。

第4行有18个反斜杠,因此没有任何问题,并显示为9个反斜杠,因为每个都被转义。如果有一个奇数,第2行的错误就会发生在第4行。

第5行的奖励字符正常通过。除了标签,一切都被允许。

第7步,重置表企鹅:

mysql> delete from penguin;

第8步,使用fields escaped by条款加载到您的表格中:

mysql> load data local infile '/home/el/foo/textfile.txt' into table penguin 
       fields terminated by '\t' escaped by '\b' 
       lines terminated by '\n' (@col1, @col2) set id=@col1, 
       chucknorris=@col2;

Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

第9步,从您的表格中选择,解释结果:

mysql> select * from penguin;
+----+------------------------------------------------------------------+
| id | chucknorris                                                      |
+----+------------------------------------------------------------------+
|  1 | aliens are on route                                              |
|  2 | scramble the nimitz\                                             |
|  3 | \its species 8472                                                |
|  4 | \\\\\\\\\\\\\\\\\\                                               |
|  5 | Bonus characters:!@#$%^&*()_+=-[]\|}{;'":/.?>,< anything but tab |
+----+------------------------------------------------------------------+
5 rows in set (0.00 sec)

现在一切都如我们所料。第2行末尾的反斜杠不会越过换行符。第3行i之前的反斜杠不执行任何操作。第4行的18个反斜杠不会被转义。奖金角色通过确认。

答案 2 :(得分:0)

您可以使用以下代码:

$yourVariable = addcslashes($_POST["your param"],"\\");

例如在我的Web表单中,我想插入本地目录:

$localAddress = addcslashes($_POST["localAddress"],"\\");