Home Database Mysql Tutorial Orace查询数据出现乱码解决办法

Orace查询数据出现乱码解决办法

Jun 07, 2016 pm 05:52 PM

下面我来介绍关于Orace查询数据出现乱码问题分析与最后乱码的解决办法,有需要学习的朋友可参考参考。

 

问题描述:

经常有些朋友会遇到,我明明是输入的正确中文,为什么我在另外一台电脑上查询却出现乱码啦?其实这个是数据库在进行字符集转换的时候出现了问题,

下面通过测试来描述具体的情况:

1.环境

Oracle 数据库字符集:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott
 
SQL> SELECT * FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME = 'NLS_CHARACTERSET';
 
PROPERTY_NAME                  PROPERTY_VALUE                                                                   DESCRIPTION
------------------------------ -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
NLS_CHARACTERSET               ZHS16GBK                                                                         Character set
 

Oracle 数据库所在的客服端字符集:

在注册表的:NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 如下图:

 

Oracle 所在的操作系统的字符集:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:UsersAndy>chcp
活动代码页: 936

表示是:中国 - 简体中文(GB2312)

2.测试

  字符集如下:

  Oracle 数据库字符集:ZHS16GBK

  Oracle 数据库客户端字符集:ZHS16GBK

  操作系统字符集:中国 - 简体中文(GB2312)

 输入测试数据:

SQL> INSERT INTO TAB_INDX
  2  values(1,'汉字输入字符集测试','Chinese Input Test',sysdate);
 
1 row inserted
 
字符集不修改,进行测试数据现实:

SQL> select * from tab_indx where tid = 1;
 
       TID TNAME                                                                            TDESC                                                                            SYSDT
---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -----------
         1 汉字输入字符集测试                                                               Chinese Input Test                                                               2012/12/30
 
显示正常,

现在我把客服端的字符集修改为:UTF8

及注册表的:NLS_LANG=SIMPLIFIED CHINESE_CHINA.UTF8

现在字符集如下:

  Oracle 数据库字符集:ZHS16GBK

 

  Oracle 数据库客户端字符集:UTF8

 

  操作系统字符集:中国 - 简体中文(GB2312)

 

现在再查询刚才输入的数据:

SQL> select tname,tdesc from tab_indx;
 
TNAME                                                                            TDESC
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
姹?瀛?杈??ュ?绗﹂??娴?璇?                                                      Chinese Input Test

发现查询出来的数据已经不能正常现实,因为这些汉字是以ZHS16GBK编码格式存储的,然而你查询出来后根据Oracle客服端的编码(UTF8)转换,及转成了UTF8的编码格式,但是操作系统是简体中文(GB2312),所以操作系统就把UTF8编码格式的数据,当成简体中文(GB2312)的编码格式数据显示,结果就出现了乱码,

现在我再插入一笔数据:

SQL> INSERT INTO TAB_INDX
  2   values(1,'UTF8下汉字输入字符集测试','Chinese Input Test',sysdate);
 
1 row inserted

再查询:

SQL> select tname,tdesc from tab_indx;
 
TNAME                                                                            TDESC
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
姹?瀛?杈??ュ?绗﹂??娴?璇?                                                      Chinese Input Test
UTF8锛??锛?锛?锛?锛??????锛?                                                     Chinese Input Test

发现新插入的数据也出现了乱码,但是乱码跟刚才的值不一样??为什么呢?

因为输入的汉字,是简体中文(GB2312)的编码格式,当Oracle数据库按照客户端的编码格式传给数据库,Oracle数据库发现,Oracle数据库客户端是UTF8的编码格式,跟数据库的编码格式(ZHS16GBK)不一样,就进行字符集转换,UTF8-->ZHS16GBK,所以把简体中文(GB2312)的编码格式的数据当成UTF8,转为ZHS16GBK的编码格式数据,就已经出错啦,查询出来自然转换回去就不行啦

(本来需要测试Oracle数据库的字符集修改后的情况,这种情况暂时不测试)这里我们在把Oracle客户的字符集修改回去;

在注册表的:NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK

 

现在字符集如下:

 

  Oracle 数据库字符集:ZHS16GBK

 

  Oracle 数据库客户端字符集:ZHS16GBK

 

  操作系统字符集:中国 - 简体中文(GB2312)

 

 

再查询:

 

 

SQL> select tname,tdesc from tab_indx;
 
TNAME                                                                            TDESC
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
汉字输入字符集测试                                                               Chinese Input Test
UTF8?????????????                                                          Chinese Input Test


发现最开始输入的汉字正常啦,但是第二次输入的汉字,又变了,跟上次的乱码不一样????

虽然这里没有进行编码格式转换,但是上次在存数据的时候,已经是存的错误的编码格式,所以显示出来肯定不正确

其实还有好几种情况测试,由于本地环境的限制,所以测试的其它情况,大家可以去试试,如:数据库的字符集是UTF8,然后客服端的字符集变化,对汉字的输入输出有什么影响

根据上面的测试情况和我自己的分析,现在总结如下:

1.数据库的查询出来的数据,是Oracle数据库字符集,Oracle客户端字符集,操作系统字符集共同作用的结果。

2.Oracle存数据和查询数据都是通过Oracle数据库的字符集和Oracle客服端的字符集进行转换的,显示数据又是根据操作系统的字符集来确定的。

3.为了避免出现乱码必须要把Oracle客户端的字符集和操作系统的字符集设置成一样的。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

See all articles