mysql LIKE 子句转义符处置

mysql LIKE 子句转义符处理

    MySQL 在字符串中使用的是C的转义句法(例如“\n”), 所以在   LIKE   字符串中使用的任何一个   “\”   必须被双写。 例如,为了查找   “\n”,必须以   “\\n”   形式指定它。为了查找   “\”,必须指定它为   “\\\\”   (反斜线被语法分析器剥离一次,另一次在模式匹配时完成,留下一条单独的反斜线被匹配)。

    private String   filtrateLikeSql(String value){

       if(null!=value){

         String newValue=””;

         newValue=value.replaceAll(“\\\\”,”\\\\\\\\”);

         newValue=newValue.replaceAll(“‘”,”\\\\'”);

         newValue=newValue.replaceAll(“_”,”\\\\_”);

          newValue=newValue.replaceAll(“\””,”\\\\\””);

          newValue=newValue.replaceAll(“%”,”\\\\%”);

         return newValue;

       }

       return value;

    }

    private String   filtrateNotLikeSql(String value){

       if(null!=value){

         String newValue=””;

         newValue=value.replaceAll(“\\\\”,”\\\\\\\\”);

          newValue=newValue.replaceAll(“\””,”\\\\\””);

         return newValue;

       }

       return value;

    }

mysql LIKE 子句转义符处置

相关文章:

你感兴趣的文章:

标签云: