MySQL没有插入反斜杠

dee*_*rox 17 mysql sql

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

id 设置为自动增量:

码:

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

如何插入文字反斜杠?

关于转义序列的注释:

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.
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 24

你需要逃避反斜杠:

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

引用(包含你必须为mysql转义的所有字符的列表)

  • @deerox:请记住这是非标准行为.其他DBMS不需要(或使用)反斜杠来转义字符. (2认同)

Eri*_*ski 6

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

第1步,创建表:

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

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

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

第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
Run Code Online (Sandbox Code Playgroud)

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

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

第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)
Run Code Online (Sandbox Code Playgroud)

第6步,想想究竟出了什么问题:

左侧的反斜杠nimitz导致mysql加载数据解析器将第2行的结尾与第3行的开头连接起来.然后它突然碰到选项卡并将'nimitz \n3'加入第2行.

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

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

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

第7步,重置表企鹅:

mysql> delete from penguin;
Run Code Online (Sandbox Code Playgroud)

步骤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
Run Code Online (Sandbox Code Playgroud)

第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)
Run Code Online (Sandbox Code Playgroud)

现在一切都如我们所料.第2行末尾的反斜杠不会越过换行符.第i3行之前的反斜杠没有做任何事情.第4行的18个反斜杠不会被转义.奖金角色通过确认.