Home > Database > Mysql Tutorial > body text

MySQL Slashes and Backslashes_MySQL

WBOY
Release: 2016-06-01 13:16:07
Original
774 people have browsed it

There was a on Twitter to @MySQLHow can I insert “/” into a varchar field please. Since they were polite and usedplease, I wanted to be helpful.

At first I thought this was the ol’ MySQL string literal problem where you need to have twobackslashes to get one backslash in a character field. SeeString Literalsin the MySQL Manual.Back Slash and Fore SlashA lot of people have trouble with the backslash when they first get started with MySQL. From the quote below, you will see that some characters need to be escaped with a backslash. But what about the forward slash?

I remember a previous job were I saved many UNIX file names but I did not remember anything unusual about forward slashed. My mental cache had been flushed of the answer. So I created a test table and experimented.

As can be shown in the image, the ‘//’ trick works for inputting a single backslash. And ‘//’ will input TWO slashes. So their is no trick for fore slashes.

No too bad for a Monday morning with no caffeine.

Within a string, certain sequences have special meaning unless the NO_BACKSLASH_ESCAPES SQL mode is enabled. Each of these sequences begins with a backslash (“/”), known as the escape character. MySQL recognizes the escape sequences shown in Table 9.1, “Special Character Escape Sequences”. For all other escape sequences, backslash is ignored. That is, the escaped character is interpreted as if it was not escaped. For example, “/x” is just “x”. These sequences are case sensitive. For example, “/b” is interpreted as a backspace, but “/B” is interpreted as “B”. Escape processing is done according to the character set indicated by the character_set_connection system variable. This is true even for strings that are preceded by an introduce that indicates a different character set, as discussed in Section 10.1.3.5, “Character String Literal Character Set and Collation”.

Table 9.1 Special Character Escape Sequences

Escape Sequence Character Represented by Sequence
An ASCII NUL (0×00) 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.

The ASCII 26 character can be encoded as “/Z” to enable you to work around the problem that ASCII 26 stands for END-OF-FILE on Windows. ASCII 26 within a file causes problems if you try to use mysql db_name

The “/%” and “/_” sequences are used to search for literal instances of “%” and “_” in pattern-matching contexts where they would otherwise be interpreted as wildcard characters. See the description of the LIKE operator in Section 12.5.1, “String Comparison Functions”. If you use “/%” or “/_” outside of pattern-matching contexts, they evaluate to the strings “/%” and “/_”, not to “%” and “_”.

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 string quoted with “'” needs no special treatment.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template