2009-05-19 9 views

答えて

78

The information provided in this answer can lead to insecure programming practices.

The information provided here depends highly on MySQL configuration, including (but not limited to) the program version, the database client and character-encoding used.

は、標準SQLではありませんので、あなたは文字列の区切り文字には二重引用符を使用して、

select * from tablename where fields like "%string \"hi\" %"; 

が、Bill Karwin notes belowとして必要http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

 
MySQL recognizes the following escape sequences. 
\0  An ASCII NUL (0x00) character. 
\'  A single quote (“'”) character. 
\"  A double quote (“"”) character. 
\b  A backspace character. 
\n  A newline (linefeed) character. 
\r  A carriage return character. 
\t  A tab character. 
\Z  ASCII 26 (Control-Z). See note following the table. 
\\  A backslash (“\”) character. 
\%  A “%” character. See note following the table. 
\_  A “_” character. See note following the table. 

を参照してください。 。これは物事を簡略化します:

select * from tablename where fields like '%string "hi" %'; 
9

mysql_real_escape_stringを使用できます。 mysql_real_escape_string()%_をエスケープしていないので、あなたは、別途MySQLのワイルドカード(%_)をエスケープする必要があります。

24

あなたは、文字列の区切り文字のための単一引用符を使用する必要があります。一重引用符は標準SQL文字列区切り文字で、二重引用符は識別子区切り文字です(テーブルや列の名前に特殊な単語や文字を使用できます)。 MySQLでは

、デフォルトでは文字列の区切り文字として二重引用符の仕事(nonstandardly)(あなたはANSI SQLモードを設定していない場合)。別のブランドのSQLデータベースを使用している場合は、標準で引用符を使用する習慣に慣れることができます。私はJavaで私自身のMySQLのエスケープ方法を開発しました

select * from tablename where fields like '%string "hi" %'; 
+0

あなたがに切り替えた場合ので、私は単一引用符の考え方に同意してDBに値を更新することができますPostgresSQLでは、あなたのクエリを頑固に拒否し、一重引用符を使用する必要があります。それは良い習慣です。 – kovacsbv

26

(:

単一引用符を使用してのもう一つの便利な利点は、あなたの文字列内のリテラルの二重引用符をエスケープする必要がないということです誰にとっても有用な場合)。

以下のクラスコードを参照してください。

警告:間違ったNO_BACKSLASH_ESCAPES SQLモードが有効になっている場合。

private static final HashMap<String,String> sqlTokens; 
private static Pattern sqlTokenPattern; 

static 
{   
    //MySQL escape sequences: http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html 
    String[][] search_regex_replacement = new String[][] 
    { 
       //search string  search regex  sql replacement regex 
      { "\u0000" ,  "\\x00"  ,  "\\\\0"  }, 
      { "'"   ,  "'"   ,  "\\\\'"  }, 
      { "\""  ,  "\""  ,  "\\\\\"" }, 
      { "\b"  ,  "\\x08"  ,  "\\\\b"  }, 
      { "\n"  ,  "\\n"  ,  "\\\\n"  }, 
      { "\r"  ,  "\\r"  ,  "\\\\r"  }, 
      { "\t"  ,  "\\t"  ,  "\\\\t"  }, 
      { "\u001A" ,  "\\x1A"  ,  "\\\\Z"  }, 
      { "\\"  ,  "\\\\"  ,  "\\\\\\\\" } 
    }; 

    sqlTokens = new HashMap<String,String>(); 
    String patternStr = ""; 
    for (String[] srr : search_regex_replacement) 
    { 
     sqlTokens.put(srr[0], srr[2]); 
     patternStr += (patternStr.isEmpty() ? "" : "|") + srr[1];    
    } 
    sqlTokenPattern = Pattern.compile('(' + patternStr + ')'); 
} 


public static String escape(String s) 
{ 
    Matcher matcher = sqlTokenPattern.matcher(s); 
    StringBuffer sb = new StringBuffer(); 
    while(matcher.find()) 
    { 
     matcher.appendReplacement(sb, sqlTokens.get(matcher.group(1))); 
    } 
    matcher.appendTail(sb); 
    return sb.toString(); 
} 
+0

mysqlプロシージャの検索置換テーブルを使用して、 '\ t'のようにdbに正しく挿入されるようにタブ区切り文字をエスケープする必要があります。 thx –

9

MySQLが文字列関数QUOTEを持っており、それがこの問題を解決する必要があります。そのような文字列の場合

4

、説明したように私のためにそれを行うための最も快適な方法は、「「倍増たりれますMySQLのマニュアルで:

There are several ways to include quote characters within a string:

A “'” inside a string quoted with “'” may be written as “''”. 

A “"” inside a string quoted with “"” may be written as “""”. 

Precede the quote character by an escape character (“\”). 

A “'” inside a string quoted with “"” needs no special treatment and need not be doubled or escaped. In the same way, “"” inside a 

Strings quoted with “'” need no special treatment.

それはhttp://dev.mysql.com/doc/refman/5.0/en/string-literals.htmlからである

0

あなたの場合。文字列で検索するときに変数を使用すると、mysql_real_escape_string()が役に立ちます。ちょっと私の提案:

$char = "and way's 'hihi'"; 
$myvar = mysql_real_escape_string($char); 

select * from tablename where fields like "%string $myvar %"; 
+0

この関数はPHP 5.5で償却され、7.0から削除されました –

0

ターミナルを使ってMYSQLに二重引用符を挿入する方法をテストするには、次の方法を使用できます。

TableName(Name,DString) - > Schema
insert into TableName values("Name","My QQDoubleQuotedStringQQ")


値を挿入した後、あなたは二重引用符または一重引用符

update table TableName replace(Dstring,"QQ","\"")

関連する問題