oracle中是否有任何函数在sql查询中转义错误的字符?我有从不同的字符串构建查询的代码,其中一些可能包含'
字符,这会破坏sql查询。
答案 0 :(得分:11)
正如Yahia所指出的,你应该总是使用绑定变量而不是动态地动态组装SQL语句。这是保护自己免受SQL注入攻击的正确方法。转义字符串可以提供更低级别的保护。
话虽如此,假设您使用的是Oracle 10.1或更高版本,则可以使用q引用语法。像
这样的东西 1 select q'[This is a string with an embedded ']' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
您可以将[和]字符替换为多个其他字符,具体取决于字符串中可能出现的字符
1 select q'<This is a string with an embedded '>' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
SQL> ed
Wrote file afiedt.buf
1 select q'{This is a string with an embedded '}' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
答案 1 :(得分:3)
你永远不应该从字符串构建查询 - 而是使用参数...否则总会有人找到一种方法来注入一些SQL。
答案 2 :(得分:2)
两个单引号一起逃脱单引号。因此,请在字符串中使用''
代替'
。