22

在 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.
4

3 回答 3

29

您需要转义反斜杠:

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

参考(包含您必须为 mysql 转义的所有字符的列表)

于 2012-10-07T16:35:26.103 回答
9

处理mysql load data infile工具中的反斜杠和所有控制字符:

第 1 步,创建您的表:

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

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

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

第五步,分析警告:

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 行的开头连接起来。然后它撞到一个制表符并将 'scramble the nimitz\n3 放入第 2 行。

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

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

第 5 行的奖励字符正常通过。除了制表符之外,一切都是允许的。

第7步,重置表企鹅:

mysql> delete from penguin;

fields escaped by第 8 步,使用以下子句加载到您的表中:

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 | spacealiens 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 个反斜杠没有被转义。并且奖金字符通过确定。

于 2014-01-17T04:58:10.823 回答
1

您可以使用此代码:

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

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

$localAddress = addcslashes($_POST["localAddress"],"\\");
于 2020-03-07T10:14:03.950 回答