1、今天写的一个存储过程,发现MYSQL的特殊字符和动态处理语句结合起来要转义好几次 。
贴出我的问题以及解决方法。
表结构:
/*DDL Information For - test.cs_test*/
--------------------------------------
Table Create Table
------- -----------------------------------------
cs_test CREATE TABLE `cs_test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(64) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
存储过程1:(静态,也就是不用预处理的部分。)
存储过程2:(用预处理部分。)
表之前的数据:
结果:
调用存储过程1:
如果你要把ID为6的记录对应的NAME字段更新为'woshiduide\n\n\n\n\n'的话,只能这样调用:
这里\n中的反斜杠是要转义的,要不然就会出现非自己想要的结果。
调用存储过程2:
结果:
这样的结果不是我们想要的。
正确调用如下:
结果:
这个要进行两次转义。具体原因等一下再说。因为我现在还不知道怎么表述才是最好的。
贴出我的问题以及解决方法。
表结构:
/*DDL Information For - test.cs_test*/
--------------------------------------
Table Create Table
------- -----------------------------------------
cs_test CREATE TABLE `cs_test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(64) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
存储过程1:(静态,也就是不用预处理的部分。)
DELIMITER $$
|
存储过程2:(用预处理部分。)
DELIMITER $$
|
表之前的数据:
select * from cs_test;
|
结果:
uery result(9 records)
id | name |
1 | csdn1 |
2 | colorful1 |
3 | bbs1 |
4 | cu1 |
5 | cu2 |
6 | woshiduide |
7 | woshicuode |
8 | I'm wrong |
9 | I'm right |
调用存储过程1:
如果你要把ID为6的记录对应的NAME字段更新为'woshiduide\n\n\n\n\n'的话,只能这样调用:
call sp_normal_test(6,'woshiduide\\n\\n\\n\\n\\n',@e);
select * from cs_test where id = 6;
|
query result(1 records)
@e |
1 |
query result(1 records)
id | name |
6 | woshiduide\n\n\n\n\n |
这里\n中的反斜杠是要转义的,要不然就会出现非自己想要的结果。
调用存储过程2:
call sp_dynamic_test(6,'woshiduide\\n\\n\\n\\n\\n',@e);
|
结果:
query result(1 records)
@e |
1 |
query result(1 records)
id | name |
6 | woshiduide |
这样的结果不是我们想要的。
正确调用如下:
call sp_dynamic_test(6,'woshiduide\\\\n\\\\n\\\\n\\\\n\\\\n',@e);
|
结果:
query result(1 records)
@e |
1 |
query result(1 records)
id | name |
6 | woshiduide\n\n\n\n\n |
这个要进行两次转义。具体原因等一下再说。因为我现在还不知道怎么表述才是最好的。
本文出自 “上帝,咱们不见不散!” 博客,转载请与作者联系!