2011-01-26 58 views
7

我正在从一个从多个文件读取数据的shell脚本中执行insert查询。有些要插入的数据包含'文本和MySQL一直给错误如何在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 

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

+0

这个线程可以帮助您提供:逃离MYSQL命令行通过Bash脚本](http://stackoverflow.com/questions/4383135/escaping-mysql-command-lines-via-bash-scripting) – jujule 2011-01-26 10:17:48

+0

它更好,如果你逃脱'字符比'\。它是因为某些db ex:sqllite显示\ escape字符的错误。你可以写:“妇女的发展与赋权,青年事务”。 – YROjha 2017-09-15 07:25:44

回答

1

呦需要躲避'用反斜杠字符\

妇女\的发展&赋权,青年事务

+0

我有大约30K的文件,我不想用sed来做一个查找和替换。如果可能的话,我宁愿在mysql端处理。 – charan 2011-01-26 10:20:45

1

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

3

在将它们传递给MySql之前,您必须先转义输入字符串。

list of escape character s是:

Character Escape 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. 
0

什么是服务器端languge您使用?

有很多方法可以让你逃脱字符串,我更喜欢这个!

例如:你可以使用mysql_escape_string数据您在查询输入所以它会自动跳脱charecters像" ' "

函数mysql_escape_string是php function您使用php