[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A string is a sequence of characters, surrounded by either single quote (`'') or double quote. Examples:
'a string' "another string" |
If the server SQL mode has ANSI_QUOTES
enabled, string literals can be
quoted only with single quotes. (A string quoted with double quotes will be
interpreted as an identifier.)
Within a string, certain sequences have special meaning. Each of these sequences begins with a backslash (`\'), known as the escape character. MySQL recognizes the following escape sequences:
\0
NUL
) character.
\'
\"
\b
\n
\r
\t
\z
mysql db_name < file_name
.)
\\
\%
\_
These sequences are case sensitive. For example, `\b' is interpreted as a backslash, but `\B' is interpreted as `B'.
Note that if you use `\%' or `\_' in some string contexts, these will return the strings `\%' and `\_' and not `%' and `_'.
There are several ways to include quotes within a string:
The SELECT
statements shown here demonstrate how quoting and
escaping work:
mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello'; +-------+---------+-----------+--------+--------+ | hello | "hello" | ""hello"" | hel'lo | 'hello | +-------+---------+-----------+--------+--------+ mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello"; +-------+---------+-----------+--------+--------+ | hello | 'hello' | ''hello'' | hel"lo | "hello | +-------+---------+-----------+--------+--------+ mysql> SELECT "This\nIs\nFour\nlines"; +--------------------+ | This Is Four lines | +--------------------+ |
If you want to insert binary data into a string column (such as a
BLOB
), the following characters must be represented by escape
sequences:
NUL
NUL
byte (ASCII 0).
Represent this character by `\0' (a backslash followed by an ASCII `0' character).
\
'
"
When writing application programs, any string that might contain any of these special characters must be properly escaped before the string is used as a data value in a SQL statement that is sent to the MySQL server. You can do this in two ways:
mysql_real_escape_string()
to escape characters.
See section 19.1.2 C API Function Overview. The Perl DBI interface provides a
quote
method to convert special characters to the proper escape
sequences. See section 19.5 MySQL Perl API.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |