2009-05-19 17 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.

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\" %"; 

हालांकि Bill Karwin notes below के रूप में की जरूरत है, स्ट्रिंग सीमांकक के लिए दोहरे उद्धरण चिह्नों का उपयोग कर मानक SQL नहीं है, तो यह एक उद्धरण चिह्नों का उपयोग अच्छा व्यवहार है । यह चीजों को सरल बनाता है:

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

आप mysql_real_escape_string का उपयोग कर सकते हैं। mysql_real_escape_string()% और _ से बच नहीं है, इसलिए आपको MySQL वाइल्डकार्ड (% और _) से अलग होना चाहिए।

24

आपको स्ट्रिंग delimiters के लिए सिंगल कोट्स का उपयोग करना चाहिए। सिंगल-कोट मानक एसक्यूएल स्ट्रिंग डिलीमीटर है, और डबल-कोट्स पहचानकर्ता डिलीमीटर हैं (इसलिए आप टेबल या कॉलम के नाम पर विशेष शब्द या अक्षर का उपयोग कर सकते हैं)।

MySQL में, डिफ़ॉल्ट रूप से स्ट्रिंग डिलीमीटर के रूप में डबल-कोट्स (गैर मानक) काम करते हैं (जब तक आप ANSI SQL मोड सेट नहीं करते)। यदि आप कभी भी SQL डेटाबेस का एक और ब्रांड उपयोग करते हैं, तो आपको मानक उद्धरणों का उपयोग करने की आदत में आने से लाभ होगा। (

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

मैं एकल उद्धरण विचार से सहमत हुई क्योंकि यदि आप करने के लिए स्विच के साथ DB में मूल्य अद्यतन कर सकते हैं , कहते हैं, PostgresSQL यह आपके प्रश्नों को जिद्दी रूप से मना कर देगा और आपको सिंगल कोट्स का उपयोग करना होगा। यह एक अच्छा अभ्यास है। – kovacsbv

26

मैं जावा में मेरे अपने MySQL भागने विधि विकसित की हैं:

एकल उद्धरण उपयोग का एक और लाभ यह है कि काम अपने स्ट्रिंग के भीतर शाब्दिक दोहरे-उद्धरण वर्ण भाग निकले जाने की जरूरत नहीं है अगर किसी के लिए उपयोगी है)।

नीचे कक्षा कोड देखें।

चेतावनी: गलत अगर 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 t' जैसे डीबी में सही ढंग से डालने के लिए एक टैब डिलीमीटर से बचने की आवश्यकता है। thx –

9

MySQL स्ट्रिंग समारोह QUOTE है, और यह इस समस्या का समाधान करना चाहिए:, सबसे अधिक आरामदायक जिस तरह से यह दोगुना कर रही है 'या ऐसा करने के लिए ", मेरे लिए

4

ऐसे ही तार के लिए के रूप में समझाया

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 के मैनुअल में एक स्ट्रिंग में खोज करते समय एक चर का उपयोग कर रहे हैं, 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","\"")

संबंधित मुद्दे