oracle转义功能

mic*_*nko 4 oracle sql-injection escaping

在oracle中是否有任何函数在sql查询中转义错误的字符?我有从不同的字符串构建查询的代码,其中一些可能包含'字符,这打破了SQL查询.

Jus*_*ave 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 '
Run Code Online (Sandbox Code Playgroud)

您可以使用许多其他字符替换[和]字符,具体取决于字符串中可能出现的字符

  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 '
Run Code Online (Sandbox Code Playgroud)

  • @Vincent - 很对.第一个会失败,如果有一个],第二个会失败,如果有一个>,第三个会失败,如果有一个}.您必须选择一个不会出现在字符串本身中的机箱字符. (2认同)