Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (99 sloc) 5.27 KB

Characters Sets and National Language Support (NLS)

Data fetched from, and sent to, Oracle Database will be mapped between the database character set and the "Oracle client" character set of the Oracle client libraries used by cx_Oracle. If data cannot be correctly mapped between client and server character sets, then it may be corrupted or queries may fail with :ref:`"codec can't decode byte" <codecerror>`. Most applications will need to specify the client character set.

cx_Oracle uses Oracle’s National Language Support (NLS) to assist in globalizing applications. As well as character set support, there are many other features that will be useful in applications. See the Database Globalization Support Guide.

Setting the Client Character Set

You can specify the Oracle client character set used by cx_Oracle by passing the encoding and nencoding parameters to the :meth:`cx_Oracle.connect` and :meth:`cx_Oracle.SessionPool` methods. For example:

import cx_Oracle
connection = cx_Oracle.connect(connectString, encoding="UTF-8",

The encoding parameter affects character data such as VARCHAR2 and CLOB columns. The nencoding parameter affects "National Character" data such as NVARCHAR2 and NCLOB. If you are not using national character types, then you can omit nencoding.

cx_Oracle will first treat the encoding parameter values as IANA encoding names. If no name is matched, it will attempt to use Oracle character set names. For example, for UTF-8 characters you should use the IANA name "UTF-8" or the Oracle name "AL32UTF8". Do not accidentally use "UTF8", which Oracle uses to specify the older Unicode 3.0 Universal character set, CESU-8.

An alternative to setting the encoding parameters is to set Oracle's NLS_LANG environment variable to a value such as AMERICAN_AMERICA.AL32UTF8. See :ref:`Setting environment variables <envset>`. As well as setting the character set, the NLS_LANG environment variable lets you specify the Language (AMERICAN in this example) and Territory (AMERICA) used for NLS globalization. See Choosing a Locale with the NLS_LANG Environment Variable.

A character set specified by an encoding parameter will override the character set in NLS_LANG. The language and territory components will still be used by Oracle.

If the NLS_LANG environment variable is set in the application with os.environ['NLS_LANG'], it must be set before any connection pool is created, or before any standalone connections are created.

Other Oracle globalization variable can also be set, see Setting NLS Parameters.

Character Set Example

The script below tries to display data containing a Euro symbol from the database. The NLS_LANG environment variable on the operating system is set to AMERICAN_AMERICA.WE8ISO8859P1:

connection = cx_Oracle.connect(userName, password, "")
cursor = connection.cursor()
for row in cursor.execute("select * from nchar_test"):

Because the '€' symbol is not supported by the WE8ISO8859P1 character set, all '€' characters are replaced by '¿' in the cx_Oracle output:

('¿', 'test      ', 'test', 'test      ')
('¿', 'test      ', '¿', 'test      ')
('"nvarchar"', '/"nchar"  ', 'varchardata', 'chardata  ')
('°', 'Second    ', 'Third', 'Fourth    ')

When the encoding parameter is set during connection:

connection = cx_Oracle.connect(userName, password, "",
        encoding="UTF-8", nencoding="UTF-8")

Then the output displays the Euro symbol as desired:

('€', 'test      ', 'test', 'test      ')
('€', 'test      ', '€', 'test      ')
('"nvarchar"', '/"nchar"  ', 'varchardata', 'chardata  ')
('°', 'Second    ', 'Third', 'Fourth    ')

Finding the Database and Client Character Set

To find the database character set, execute the query:

SELECT value AS db_charset
FROM nls_database_parameters

To find the current "client" character set used by cx_Oracle, execute the query:

SELECT DISTINCT client_charset AS client_charset
FROM v$session_connect_info

If these character sets do not match, characters will be mapped when transferred over Oracle Net. This may impact performance and may result in invalid data.

You can’t perform that action at this time.