• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL Slashes and Backslashes_MySQL

mysql 搞代码 4年前 (2022-01-09) 12次浏览 已收录 0个评论

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.A 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 newlin

本文来源gaodai.ma#com搞#代!码网_

e (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 < file_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.


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL Slashes and Backslashes_MySQL

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址