如何在 MySQL 中插入一个特殊字符,例如 '?

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

How do I insert a special character such as ' into MySQL?

mysql

提问by charan

I am executing the insertquery from a shell script which reads data from multiple files. Some of the data to be inserted contains 'in the text and MySQL keeps giving errors

我正在insert从一个 shell 脚本执行查询,该脚本从多个文件中读取数据。一些要插入的数据包含'在文本中,MySQL 不断报错

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Development & Empowerment, Youth Affairs                 
','
Himachal Pradesh                    ' at line 1

This is the actual text: Women's Development & Empowerment, Youth Affairs.

这是实际的文本:Women's Development & Empowerment, Youth Affairs

回答by Sebastian Paaske T?rholm

You need to escape the quote, like so:

您需要转义引用,如下所示:

'Women\'s Development & Empowerment, Youth Affairs'

Note, that if you're generating the SQL statement from a language like PHP, there are functions available to do this for you.

请注意,如果您从 PHP 之类的语言生成 SQL 语句,则有一些函数可以为您执行此操作。

In PHP, for instance, there is mysql_real_escape_string, which takes care of it for you. Note, that prepared statements are to be prefered over this, as it's harder to get those wrong.

例如,在 PHP 中有mysql_real_escape_string,它会为您处理它。请注意,准备好的语句比这个更受欢迎,因为更难弄错。

See also:

也可以看看:

回答by RobinG

You will have to escape the input strings before passing them to MySql.

在将输入字符串传递给 MySql 之前,您必须对其进行转义。

The list of escape characters is:

转义字符的列表s是:

Character   Escape Sequence
##代码##  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 sissonb

Yo need to escape the ' character with a backslash \

你需要用反斜杠 \ 转义 ' 字符

Women\'s Development & Empowerment, Youth Affairs

妇女发展与赋权,青年事务

回答by borrible

You need to escape your quote. You can do this by doubling it in your insert query; i.e. use '' rather than '. That is two single quotes rather than a single double quote.

你需要逃避你的报价。您可以通过在插入查询中将其加倍来做到这一点;即使用''而不是'。那是两个单引号而不是一个双引号。

回答by Harish

What is the server side languge you using?

您使用的服务器端语言是什么?

there are many methods through which you can escape string , i'll prefer that!

有很多方法可以转义 string ,我更喜欢!

Eg: you can use mysql_escape_stringthe data you are entering in query so it will automatically escape charecters like " ' "

例如:您可以使用mysql_escape_string您在查询中输入的数据,因此它会自动转义字符,例如" ' "

mysql_escape_string is a php functionprovided you are using php

mysql_escape_string 是php function您正在使用的提供php