search
HomeDatabaseMysql TutorialOrace查询数据出现乱码的问题解决思路

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

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

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:\Users\Andy>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
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
发现查询出来的数据已经不能正常现实,因为这些汉字是以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
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
发现新插入的数据也出现了乱码,但是乱码跟刚才的值不一样??为什么呢?
因为输入的汉字,是简体中文(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
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
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.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 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

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 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.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment