Oracle中字符集的学习
字符集在我们日常上网,文件操作等很多方面都有应用,而我们自己的电脑通常是客户端,在上网时曾出现乱码或不能识别的字符,打开word也可能会出现这种情况,今天就从日常的操作开始学习:
一.认识字符集:
平时上网如果保存文档,可能我们不会关注底下的编码一栏(如图1),通常这栏式默认的。图1中保存一网页,默认格式是UTF-8的。再用浏览器打开保存的这个网页,如图2,打开时显示的格式不想以前那样,字没有以前的大,格式也没以前的漂亮。但没有丢失数据,没有乱码。如图3中,表情下面汉字正常。
图1
图2
图3
但如果保存时不用默认格式,而是用中文格式,如GB2312,则显示如图4,网页格式同保存时一样,但出现了如图5的乱码。可能这种情况大家也碰到过的。通过浏览器上面的“查看”,“编码”可以看到编码的格式是自动选择,但默认的是GB2312.因为是中文环境。
图4
图5
不同编码保存网页产生不同的结果,因为UTF8是国际编码,用UTF8编码,英文版本的IE也可以正确显示中文,而用GB2312的话,中文的网页就很好,但不一定英文版本的IE可以正常访问。本实验的凤凰网是国际网站,所以用了UTF8.
二.Oracle的字符集
通常访问Oracle数据库涉及到三个方面的字符集:服务器上数据库的字符集,客户端字符集,客户端环境变量NLS_LANG的字符集。服务器上的字符集在创建数据库时指定,一般不更改。客户端的字符集由客户端的操作系统决定,客户端环境变量NLS_LANG的字符集由用户根据情况设定。
1. 服务器上数据库的字符集
SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 12月 16 10:17:06 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn scott/m1234;
已连接。
SQL> select name,value$ from props$ where name like '%NLS%';
包含下面一项,这一项就是数据库当前的字符集
NAME
------------------------------
VALUE$
--------------------------------------------------------------------------
NLS_CHARACTERSET
ZHS16GBK
通常中文简体有两个字符集很重要:中文字符集:CHARACTER SET ZHS16GBK 和国家字符集:NATIONAL CHARACTER SET AL16UTF16.字符集文件对应专门文件lx00023.nlb和lx20354.nlb。通过LOCALE bullder可以打开这些文件查看。
请看如下SQL:
SQL> select dump('王') from dual;
DUMP('王')
---------------------
Typ=96 Len=2: 205,245
SQL> select concat( to_char(205,'xx'),to_char(245,'xx')) from dual;
CONCAT
------
cd f5
汉字“王”转换为ZHS16GBK的编码为cd f5,可在lx20354.nlb下查询到。
2. 客户端的字符集:
Windows下运行CMD,输入CHCP,可看到代码页为936。然后打开控制面板,打开“区域和语言选项”,选“高级”,可看到代码页转换表,查936,发现是GBK,说明这个客户端使用的是中文GBK编码。
3.客户端环境变量NLS_LANG。可通过如下方式找到:
三.字符集问题
1中文字符不被识别,客户端环境变量NLS_LANG 的字符集由ZHS16GBK改为US7ASCII。开始时NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>sqlplus scott/m1234
SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 12月 20 10:59:29 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> insert into test values('测');
已创建 1 行。
SQL> select test,dump (test) from test;
TE
--
DUMP(TEST)
--------------------------------------------------------------------------------
测
Typ=96 Len=2: 178,226
SQL> exit;
从 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options 断开
重新设置NLS_LANG.
C:\Documents and Settings\Administrator>set NLS_LANG=AMERICAN_AMERICA.US7ASCII
C:\Documents and Settings\Administrator>sqlplus scott/m1234
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 20 11:47:17 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select test,dump(test) from test;
TE
--
DUMP(TEST)
--------------------------------------------------------------------------------
?
Typ=96 Len=2: 178,226
SQL> exit;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
C:\Documents and Settings\Administrator>set NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
C:\Documents and Settings\Administrator>sqlplus scott/m1234
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 20 11:53:52 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select test,dump(test) from test;
TE
--
DUMP(TEST)
--------------------------------------------------------------------------------
测
Typ=96 Len=2: 178,226
2.乱码。客户端环境变量NLS_LANG 的字符集由ZHS16GBK改为WE8ISO8859P1。
SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 12月 20 12:11:12 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create table charset (name varchar2(20));
表已创建。
SQL> insert into charset values('天天向上不断进步');
已创建 1 行。
SQL> select * from charset;
NAME
--------------------
天天向上不断进步
SQL> exit;
从 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options 断开
C:\Documents and Settings\Administrator>set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
C:\Documents and Settings\Administrator>sqlplus scott/m1234
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 20 12:17:05 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from charset;
NAME
--------------------
靠靠靠靠
SQL> select name,dump(name) from charset;
NAME
--------------------
DUMP(NAME)
--------------------------------------------------------------------------------
靠靠靠靠
Typ=1 Len=16: 204,236,204,236,207,242,201,207,178,187,182,207,189,248,178,189
以上问题主要是NLS_LANG设置有问题引起的,所以这个参数设定时很重要。要小心。
可通过以下方式查看当前的字符集:
C:\>lcsscan format=html file=地产首富王健林:未来10年房价难下降_财经_凤凰网.htm
Language and Character Set File Scanner v2.1
(c) Copyright 2003, 2004 Oracle Corporation. All rights reserved.
地产首富王健林:未来10年房价难下降_财经_凤凰网.htm: SIMPLIFIED CHINESE UTF8;
Lcsscan是Oracle上面用于查看字符集的工具。
参考阅读:
http://www.itstreets.com/post/34.html
Oracle字符集之研究
http://blog.sina.com.cn/s/blog_613126600100eoa3.html
Oralce 字符集浅谈
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22528444/viewspace-682859/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22528444/viewspace-682859/