In a string, if a sequence has a special meaning, each sequence starts with a backslash ("\") and is called an escape character.
MySQL recognizes the following escape characters:
\ 0 is an ASCII 0 (NUL) character.
\ 'One ASCII 39 single quotes ('') character.
\ "An ASCII 34 double quotation mark (" ") character.
\ B is an ASCII 8 return character.
\ N is an ASCII 10 line break.
\ R: An ASCII 13 carriage return.
\ T an ASCII 9 tab ).
\ Z ascii (26) (Control-Z ). This character can be used to handle the problem that ASCII (26) represents the end of a file in Windows (ASCII (26) may cause problems when MySQL database <FILENAME) is used ).
\ An ASCII 92 backslash ("\") character.
\ % Is an ASCII 37 "%" character. It is used to search for the text instance of "%" in the body. Otherwise, "%" is interpreted as a wildcard.
\ _ An ASCII 95 '_' character. It is used to search for the text instance of "_" in the body. Otherwise, "_" is interpreted as a wildcard.
NOTE: If "\ %" or "\ _" is used in some body environments, the strings "\ %" and "\ _" are returned instead of "%" and "_".
Strings that contain quotation marks can be written in the following ways:
? A string is referenced by a single quotation mark ('). The single quotation mark (') character in the string can be escaped by a single quotation mark.
? A string is referenced by double quotation marks (""). The character "" In this string can be escaped.
? You can also add an escape character "\" before the quotation marks to escape.
? A string is referenced by double quotation marks (""). The single quotation marks (') in the string do not need to be treated specially and do not need to be repeated or escaped. Similarly, if a string is referenced by single quotation marks ('), the double quotation marks ("") in the string do not need to be treated specially and do not need to be repeated or escaped.
The following SELECT statement demonstrates how quotes and escaping work:
Mysql> select 'hello', '"hello"', '"hello" "', 'Hello', '\ 'hello ';
+ ------- + --------- + ----------- + -------- +
| Hello | "hello" | "hello" "| El 'lo | 'Hello |
+ ------- + --------- + ----------- + -------- +
Mysql> select "hello", "'hello'", "'hello'", "El" "Lo", "\" hello ";
+ ------- + --------- + ----------- + -------- +
| Hello | 'hello' | El "lo |" Hello |
+ ------- + --------- + ----------- + -------- +
Mysql> select "This \ NIS \ nfour \ nlines ";
+ -------------------- +
| This
Is
Four
Lines |
+ -------------------- +
If you want to insert binary data into a character-type field (such as BLOB), the following characters must be represented by the escape sequence:
Nul ASCII 0, you should use "\ 0" (a backslash and an ASCII "0" character) to represent it.
\ ASCII 92, backslash. It must be represented.
'Ascii 39, single quotes. It must be represented.
"ASCII 34, double quotation marks. It must be represented.
If you write c Code You can use the c api function mysql_real_escape_string () to escape characters for the insert statement. In Perl, you can use the quote method in the DBI package to convert these special characters into appropriate escape sequences.
You should use the escape function in any string that may contain the preceding special characters!
In addition, many MySQL APIs provide some placeholder processing capabilities, which allow you to insert special tags in the query statement and then bind data values to them when executing the query. In this way, the API will automatically convert them from numeric values.