MySQL 没有插入反斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12770704/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:07:10  来源:igfitidea点击:

MySQL is not inserting a backslash

mysqlsql

提问by deerox

In MySQL, when I try to insert a backslash into my table, it does not accept it and gives me the content without the backslash.

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

idis set to auto increment:

id设置为自动递增:

Code:

代码:

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

How do I insert a literal backslash?

如何插入文字反斜杠?

Notes about escape sequences:

关于转义序列的注意事项:

Escape  Sequence    Character Represented by Sequence

INSERT INTO gender
(sex, date) VALUES (
'male are allowed \ female are not allowed',
"2012-10-06")
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.

回答by Denys Séguret

You need to escape your backslash :

你需要逃避你的反斜杠:

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

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

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

回答by Eric Leschinski

How to tame backslashes in mysql load data infile tool:

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

Step 1, create your table:

第 1 步,创建您的表:

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

Step 2, create your file to import and put this data in there.

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

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

Step 3, insert into your table:

第 3 步,插入到您的表中:

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

Step 4, and of course, it causes this strange output:

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

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)

Step 5, analyze the warning:

第五步,分析警告:

mysql> delete from penguin;

Step 6, think about exactly what went wrong:

第6步,想想到底哪里出了问题:

The backslash to the left of nimitzcaused the mysql load data parser to concatenate the end of line 2 with the beginning of line 3. Then it bumped up against a tab and put 'scramble the nimitz\n3 into row 2.

左侧的反斜杠nimitz导致 mysql 加载数据解析器将第 2 行的末尾与第 3 行的开头连接起来。然后它碰到一个制表符并将 'scramble the nimitz\n3 放入第 2 行。

The rest of row 3 is skipped because the extra words its species 8472do not fit anywhere, it produces the warning you see above.

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

Row 4 had 18 backslashes, so there is no problem, and shows up as 9 backslahes because each was escaped. Had there been an odd number, the error on row 2 would have happened to row 4.

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

The bonus characters on row 5 came through normally. Everything is allowed except tab.

第 5 行的奖励字符正常通过。除了选项卡之外的所有内容都是允许的。

Step 7, reset table penguin:

第七步,重置表企鹅:

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

Step 8, load into your table with the fields escaped byclause:

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

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)

Step 9, select from your table, interpret the results:

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

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

And now everything is as we expect. The backslash at the end of line 2 does not escape the newline. The backslash before ion row 3 doesn't do anything. The 18 backslashes on row 4 are not escaped. And the bonus characters come through ok.

现在一切都如我们所料。第 2 行末尾的反斜杠不会转义换行符。第i3 行之前的反斜杠没有任何作用。第 4 行的 18 个反斜杠未转义。并且奖金字符通过确定。

回答by REZA

you can use this code :

您可以使用此代码:

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

for example in my web form i want insert local directory :

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

##代码##