关于MYSQL 字符转义问题总结

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:(静态,也就是不用预处理的部分。)


DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_normal_test`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_normal_test`(IN f_id int,
 IN f_name varchar(64),OUT f_error_code boolean)
BEGIN
  -- Determinate whether update is successful or failed.
  declare cnt int default 0;
  -- Update to new record.
  update cs_test
  set `name` = f_name
  where id = f_id;
  -- Get the affective rows.
  set cnt = row_count();
  -- Get the last record.
  if cnt > 0 then
    set f_error_code = TRUE;
   end if;
END$$

DELIMITER ;

存储过程2:(用预处理部分。)


DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_dynamic_test`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_dynamic_test`(IN f_id int,
 IN f_name varchar(64),OUT f_error_code boolean)
BEGIN
  -- Determinate whether update is successful or failed.
  declare cnt int default 0;
  -- Update to new record.
  set @stmt = concat('update cs_test set `name` =''',f_name,'''');
  set f_error_code = FALSE;
  if f_id != 0 then
    set @stmt = concat(@stmt, ' where id = ',f_id);
  end if;
  prepare s1 from @stmt;
  execute s1;
  -- Must be above of the deallocate statement.
  set cnt = row_count();
  deallocate prepare s1;
  -- Get the last record.
  if cnt > 0 then
    set f_error_code = TRUE;
   end if;
END$$

DELIMITER ;

表之前的数据:

select * from cs_test;

结果:

uery result(9 records)

idname
1csdn1
2colorful1
3bbs1
4cu1
5cu2
6woshiduide
7woshicuode
8I'm wrong
9I'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 @e;
select * from cs_test where id = 6;

query result(1 records)

@e
1

query result(1 records)

idname
6woshiduide\n\n\n\n\n


这里\n中的反斜杠是要转义的,要不然就会出现非自己想要的结果。
调用存储过程2:

call sp_dynamic_test(6,'woshiduide\\n\\n\\n\\n\\n',@e);
select @e;
select * from cs_test where id = 6;

结果:

query result(1 records)

@e
1

query result(1 records)

idname
6woshiduide

这样的结果不是我们想要的。
正确调用如下:

call sp_dynamic_test(6,'woshiduide\\\\n\\\\n\\\\n\\\\n\\\\n',@e);
select @e;
select * from cs_test where id = 6;

结果:

query result(1 records)

@e
1

query result(1 records)

idname
6woshiduide\n\n\n\n\n


这个要进行两次转义。具体原因等一下再说。因为我现在还不知道怎么表述才是最好的。

本文出自 “上帝,咱们不见不散!” 博客,转载请与作者联系!

转载于:https://my.oschina.net/u/585111/blog/219446

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值