学习手册之MySQL语言参考

来源:互联网 发布:淘宝联盟为什么要认证 编辑:程序博客网 时间:2024/04/27 15:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
7MySQL语言参考

7.1文字:怎么写字符串和数字
7.1.1字符串

一个字符串是一个字符序列,由单引号(“'”)或双引号(“"”)字符(后者只有你不在ANSI模式运行)包围。例如:

'astring'
"anotherstring"

在字符串内,某个顺序有特殊的意义。这些顺序的每一个以一条反斜线(“/”)开始,称为转义字符。MySQL识别下列转义字符:

/0
一个ASCII0(NUL)字符。
/n
一个新行符。
/t
一个定位符。
/r
一个回车符。
/b
一个退格符。
/'
一个单引号(“'”)符。
/"
一个双引号(“"”)符。
//
一个反斜线(“/”)符。
/%
一个“%”符。它用于在正文中搜索“%”的文字实例,否则这里“%”将解释为一个通配符。
/_
一个“_”符。它用于在正文中搜索“_”的文字实例,否则这里“_”将解释为一个通配符。

注意,如果你在某些正文环境中使用“/%”或“/%_”,这些将返回字符串“/%”和“/_”而不是“%”和“_”。

有几种方法在一个字符串内包括引号:

*一个字符串内用“'”加引号的“'”可以被写作为“''”。
*一个字符串内用“"”加引号的“"”可以被写作为“""”。
*你可以把一个转义字符(“/”)放在引号前面。
*一个字符串内用“"”加引号的“'”不需要特殊对待而且不必被重复或转义。同理,一个字符串内用“'”加引号的与“"”也不需要特殊对待。

下面显示的SELECT演示引号和转义如何工作:

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|
+--------------------+


如果你想要把二进制数据插入到一个BLOB列,下列字符必须由转义序列表示:

NUL
ASCII0。你应该用'/0'(一个反斜线和一个ASCII'0')表示它。
/
ASCII92,反斜线。用'//'表示。
'
ASCII39,单引号。用“/'”表示。
"
ASCII34,双引号。用“/"”表示。

如果你写C代码,你可以使用CAPI函数MySQL_escape_string()来为INSERT语句转义字符。见20.3CAPI函数概述。在Perl中,你可以使用DBI包中的quote方法变换特殊的字符到正确的转义序列。见20.5.2DBI接口。共30页  1                             
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击