今天碰到了一个麻烦的问题,向数据库中插入语句时,String的变量中有单引号,导致插入失败,竟在网上查看得到了解决方案。
以下是解决方案:
使用如下的更新语句不行:update device set dept='select * from message where name='tom' ' ;
解决方法:
update device set dept='select * from message where name=' 'tom' ' where id=3' ;
执行成功。
原来,两个'' 表示了一个'
将一个单引号换成两个可以解决。
双引号问题:
如果是双引号是这样,假如你有个表的字段叫sysdate,因为sysdate属于oracle中的特有字符,但你要查询这个字段的时候
select "sysdate" from table_name;
但是你要是用
select 'sysdate' from table_name;
那么你查出来的就是字符串,结果是sysdate
一般的程序还是不折磨人,最折磨人的就是特殊符号了,现在终于领略到了。。。
以下是解决方案:
使用如下的更新语句不行:update device set dept='select * from message where name='tom' ' ;
解决方法:
update device set dept='select * from message where name=' 'tom' ' where id=3' ;
执行成功。
原来,两个'' 表示了一个'
将一个单引号换成两个可以解决。
双引号问题:
如果是双引号是这样,假如你有个表的字段叫sysdate,因为sysdate属于oracle中的特有字符,但你要查询这个字段的时候
select "sysdate" from table_name;
但是你要是用
select 'sysdate' from table_name;
那么你查出来的就是字符串,结果是sysdate
一般的程序还是不折磨人,最折磨人的就是特殊符号了,现在终于领略到了。。。
本文介绍了解决在SQL语句中遇到单引号和双引号问题的方法,通过正确转义单引号及使用双引号绕过关键字冲突,确保数据能够顺利插入或查询。
141

被折叠的 条评论
为什么被折叠?



