在insert语句中转义

时间:2011-03-09 12:41:05

标签: mysql

如何在mysql插入语句中转义逗号?

mysql> create table test.todel (name varchar(100));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into test.todel values ('mcdonald's pizza');
    '> ';
ERROR 1064 (42000): 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 pizza');
'' at line 1

mysql> select  * from test.todel;
Empty set (0.02 sec)

我可以逃避单个逗号,但这不是一个选项,因为我使用相当复杂的shell脚本来插入数据。

mysql> insert into test.todel values ('mcdonald''s pizza');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test.todel;
+------------------+
| name             |
+------------------+
| mcdonald's pizza | 
+------------------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:1)

使用mysql_real_escape_string安全地进行搜索。

你的方式不安全BTW。

http://www.w3schools.com/php/func_mysql_real_escape_string.asp