解决方法
正如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 '