将国家字符插入 oracle NCHAR 或 NVARCHAR 列不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6509751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting national characters into an oracle NCHAR or NVARCHAR column does not work
提问by KarlP
When inserting strings in an oracle database, some national characters are replaced with question marks, even though they are inserted in an NCHAR or NVARCHAR column - that should be able to handle all Unicode characters.
在 oracle 数据库中插入字符串时,一些国家字符被替换为问号,即使它们被插入到 NCHAR 或 NVARCHAR 列中 - 这应该能够处理所有 Unicode 字符。
This happens using either Oracle's SQL Developer, sqlplus or using the JDBC driver.
使用 Oracle 的 SQL Developer、sqlplus 或使用 JDBC 驱动程序会发生这种情况。
The database NLS_CHARACTERSET is set to WE8ISO8859P1 (western european iso-8859-1) The NLS_NCHAR_CHARACTERSET used for NCHAR columns is set to AL16UTF16. (UTF-16)
数据库 NLS_CHARACTERSET 设置为 WE8ISO8859P1(西欧 iso-8859-1) 用于 NCHAR 列的 NLS_NCHAR_CHARACTERSET 设置为 AL16UTF16。(UTF-16)
Any character not in the NLS_CHARACTERSET seems to be replaced with a inverted question mark.
任何不在 NLS_CHARACTERSET 中的字符似乎都被替换为倒问号。
回答by KarlP
Edit: Note that the best way to handle UTF on Oracle is to create the database using the database character set AL32UTF8, and use ordinary varchar2 columns. One of the problems with using nchar columns is that oracle can't use indexes for ordinary char/varchar2 columns when arguments are sent as nchar by default.
编辑:请注意,在 Oracle 上处理 UTF 的最佳方法是使用数据库字符集 AL32UTF8 创建数据库,并使用普通的 varchar2 列。使用 nchar 列的问题之一是,当参数默认作为 nchar 发送时,oracle 无法为普通的 char/varchar2 列使用索引。
Anyway: If you can't convert the database:
无论如何:如果您无法转换数据库:
First, unicode literals needs to be prefixed with an 'n', like this:
首先,unicode 文字需要以“n”为前缀,如下所示:
select n'Language - Spr?k - J?zyk' from dual;
*) 8-bit encodings can't handle this text
*) 8 位编码无法处理此文本
Unfortunately, that is not enough.
不幸的是,这还不够。
For some reason, the default behaviour for database clients is to translate all string literals to the database character set, meaning that values will be changed even before the database gets to see the string.
出于某种原因,数据库客户端的默认行为是将所有字符串文字转换为数据库字符集,这意味着即使在数据库看到字符串之前,值也会更改。
The clients need some configuration in order to be able to insert a unicode character into an NCHAR or NVARCHAR column:
客户端需要一些配置才能将 unicode 字符插入到 NCHAR 或 NVARCHAR 列中:
SQL Plus on Unix
Unix 上的 SQL Plus
These environemnet variables sets up the unix environment and sqlplus to use UTF-8 files, and also configure sqlplus to send string literals in unicode.
这些环境变量将 unix 环境和 sqlplus 设置为使用 UTF-8 文件,并将 sqlplus 配置为以 unicode 发送字符串文字。
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
LC_CTYPE="en_US.UTF-8"
ORA_NCHAR_LITERAL_REPLACE=true
(en_US.UTF-8 is for Solaris - Linux or other systems may need different strings, use locale -a
to list supported locales.)
(en_US.UTF-8 适用于 Solaris - Linux 或其他系统可能需要不同的字符串,用于locale -a
列出支持的语言环境。)
JDBC Driver
JDBC 驱动程序
Applications using Oracles JDBC driver needs to have the following system property defined to send strings literals in unicode.
使用 Oracles JDBC 驱动程序的应用程序需要定义以下系统属性以在 unicode 中发送字符串文字。
-Doracle.jdbc.defaultNChar=true
-Doracle.jdbc.convertNcharLiterals=true
SQL Developer
SQL 开发人员
Locate sqldeveloper.conf, and add the following lines:
找到 sqldeveloper.conf,并添加以下几行:
AddVMOption -Doracle.jdbc.defaultNChar=true
AddVMOption -Doracle.jdbc.convertNcharLiterals=true
SQL Plus on Microsoft Windows
Microsoft Windows 上的 SQL Plus
I haven't tried if SQLplus on Microsoft Windows or Toad handles utf-8 at all. Sqlplusw.exe may do that, and the following registry settings may do the trick.
我还没有尝试过 Microsoft Windows 或 Toad 上的 SQLplus 是否完全处理 utf-8。Sqlplusw.exe 可能会这样做,而以下注册表设置可能会起作用。
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
ORA_NCHAR_LITERAL_REPLACE=true
回答by Simon Clewer
Thanks KarlP - that got me going. Recapping what worked for me.
谢谢 KarlP - 这让我走了。回顾一下对我有用的东西。
Inserting chinese ( any utf8 ) text into an nvarchar column of a non-unicode database ( eg: ISO8859 etc ), using sqlplus on linux.
在 linux 上使用 sqlplus 将中文(任何 utf8)文本插入到非 unicode 数据库(例如:ISO8859 等)的 nvarchar 列中。
These db params on my system, note a single byte encoding for char, but multibyte for nchare.
NLS_CHARACTERSET WE8ISO8859P1
NLS_NCHAR_CHARACTERSET AL16UTF16
我系统上的这些 db 参数,请注意 char 的单字节编码,但 nchare 的多字节编码。NLS_CHARACTERSET WE8ISO8859P1
NLS_NCHAR_CHARACTERSET AL16UTF16
eg:
例如:
INSERT INTO tt values ( N'气前照灯' );
The 'N' prepending the string is important. Also, must set the env before starting sqlplus,
字符串前面的“N”很重要。此外,必须在启动 sqlplus 之前设置 env,
# Important to tell sqldeveloper what encoding is needed.
export NLS_LANG=AMERICAN_AMERICA.UTF8
# Others might find AMERICAN_AMERICA.AL32UTF8 or whatever better suits.
# ** THIS MATTERS - DOES NOT WORK WITHOUT !!
export ORA_NCHAR_LITERAL_REPLACE=true