Home Database Mysql Tutorial Orace查询数据出现乱码的问题解决思路

Orace查询数据出现乱码的问题解决思路

Jun 07, 2016 pm 05:56 PM
Garbled characters Query data

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

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

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

How to solve garbled word page numbers How to solve garbled word page numbers Jun 25, 2023 pm 03:23 PM

Solution to garbled word page numbers: 1. Open the word document and click the "File" option in the upper left corner; 2. Select the "More" option, and then click the "Options" button; 3. Select "Advanced" in the word options; 4. . Find "Show field codes instead of field values" in "Show document content", remove the check in front, and click OK to return to the home page.

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

What to do if linux tty has Chinese garbled characters What to do if linux tty has Chinese garbled characters Mar 16, 2023 am 09:20 AM

Solution to Chinese garbled characters in Linux tty: 1. Download the font fbterm through the "sudo apt-get install fbterm" command; 2. Execute the "sudo fbterm" command; 3. Change the font and font size to "font-names=Ubuntu Mono font- size=14” is enough.

How to solve tomcat startup garbled code How to solve tomcat startup garbled code Dec 26, 2023 pm 05:21 PM

Solutions to garbled tomcat startup: 1. Modify Tomcat's conf configuration file; 2. Modify the system language; 3. Modify the command line window encoding; 4. Check the Tomcat server configuration; 5. Check the project encoding; 6. Check the log file; 7 , try other solutions. Detailed introduction: 1. Modify Tomcat's conf configuration file, open Tomcat's conf directory, find the "logging.properties" file, etc.

How to solve the problem of Chinese garbled characters in Windows 10 How to solve the problem of Chinese garbled characters in Windows 10 Jan 16, 2024 pm 02:21 PM

In the Windows 10 system, garbled characters are common. The reason behind this is often that the operating system does not provide default support for some character sets, or there is an error in the set character set options. In order to prescribe the right medicine, we will analyze the actual operating procedures in detail below. How to solve Windows 10 garbled code 1. Open settings and find "Time and Language" 2. Then find "Language" 3. Find "Manage Language Settings" 4. Click "Change System Regional Settings" here 5. Check the box as shown and click Just make sure.

How to solve the problem of garbled characters in win11 system documents How to solve the problem of garbled characters in win11 system documents Jun 29, 2023 pm 06:29 PM

How to solve the problem of garbled text documents in win11? When many users use the win11 system, text documents are garbled and cannot be read normally. Many friends do not know how to solve this problem. In fact, this method is not difficult. Below, the editor has compiled the steps to solve the problem of garbled Windows 11 system documents. I hope it can bring you some inspiration! Steps to solve garbled Windows 11 system documents: 1. First, open the control panel of win11, enter control panel in the search box below, and click Search to enter the control panel. 2. After entering the panel, find the clock and area and click to enter, then click on the area option. 3. After entering, click on the management panel, and then click on Change system regional settings.

Editing method to solve the problem of garbled characters when opening dll files Editing method to solve the problem of garbled characters when opening dll files Jan 06, 2024 pm 07:53 PM

When many users use computers, they will find that there are many files with the suffix dll, but many users do not know how to open such files. For those who want to know, please take a look at the following details. Tutorial~How to open and edit dll files: 1. Download a software called "exescope" and download and install it. 2. Then right-click the dll file and select "Edit resources with exescope". 3. Then click "OK" in the pop-up error prompt box. 4. Then on the right panel, click the "+" sign in front of each group to view the content it contains. 5. Click on the dll file you want to view, then click "File" and select "Export". 6. Then you can

Solve the problem of garbled characters in win11 notepad Solve the problem of garbled characters in win11 notepad Jan 05, 2024 pm 03:11 PM

Some friends want to open a notepad and find that their win11 notepad is garbled and don't know what to do. In fact, we generally only need to modify the region and language. Win11 Notepad is garbled: First step, use the search function, search and open "Control Panel" Second step, click "Change date, time or number format" under Clock and Region Third step, click the "Manage" option above Card. The fourth step is to click "Change System Regional Settings" below. The fifth step is to change the current system regional settings to "Chinese (Simplified, China)" and click "OK" to save.

See all articles