|
4 | 4 | Character Sets and Globalization |
5 | 5 | ******************************** |
6 | 6 |
|
| 7 | +Character Sets |
| 8 | +============== |
| 9 | + |
| 10 | +Database Character Set |
| 11 | +---------------------- |
| 12 | + |
7 | 13 | Data fetched from and sent to Oracle Database will be mapped between the |
8 | | -database character set and the "Oracle client" character set of the Oracle |
9 | | -Client libraries used by python-oracledb. If data cannot be correctly mapped between |
10 | | -client and server character sets, then it may be corrupted or queries may fail |
11 | | -with :ref:`"codec can't decode byte" <codecerror>`. |
| 14 | +`database character set |
| 15 | +<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-EA913CC8-C5BA-4FB3-A1B8-882734AF4F43>`__ |
| 16 | +and the "Oracle client" character set of the Oracle Client libraries used by |
| 17 | +python-oracledb. If data cannot be correctly mapped between client and server |
| 18 | +character sets, then it may be corrupted or queries may fail with :ref:`"codec |
| 19 | +can't decode byte" <codecerror>`. |
| 20 | + |
| 21 | +All database character sets are supported by the python-oracledb. |
| 22 | + |
| 23 | +.. _findingcharset: |
| 24 | + |
| 25 | +To find the database character set, execute the query: |
| 26 | + |
| 27 | +.. code-block:: sql |
| 28 | +
|
| 29 | + SELECT value AS db_charset |
| 30 | + FROM nls_database_parameters |
| 31 | + WHERE parameter = 'NLS_CHARACTERSET'; |
12 | 32 |
|
13 | | -All database character sets are supported by the python-oracledb Thick mode. |
14 | | -The database performs any required conversion for the python-oracledb Thin |
15 | | -mode. |
| 33 | +Database National Character Set |
| 34 | +------------------------------- |
16 | 35 |
|
17 | | -For the `national character set |
| 36 | +For the secondary `national character set |
18 | 37 | <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-4E12D991-C286-4F1A-AFC6-F35040A5DE4F>`__ |
19 | 38 | used for NCHAR, NVARCHAR2, and NCLOB data types: |
20 | 39 |
|
21 | 40 | - AL16UTF16 is supported by both the python-oracledb Thin and Thick modes |
22 | 41 | - UTF8 is not supported by the python-oracledb Thin mode |
23 | 42 |
|
24 | | -Python-oracledb Thick mode uses Oracle's National Language Support (NLS) to |
25 | | -assist in globalizing applications, see :ref:`thicklocale`. |
26 | | - |
27 | | -.. note:: |
| 43 | +To find the database's national character set, execute the query: |
28 | 44 |
|
29 | | - All NLS environment variables are ignored by the python-oracledb Thin mode. |
30 | | - Also the ``ORA_SDTZ`` and ``ORA_TZFILE`` variables are ignored. See |
31 | | - :ref:`thindatenumber`. |
| 45 | +.. code-block:: sql |
32 | 46 |
|
33 | | -For more information, see the `Database Globalization Support Guide |
34 | | -<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=NLSPG>`__. |
| 47 | + SELECT value AS db_ncharset |
| 48 | + FROM nls_database_parameters |
| 49 | + WHERE parameter = 'NLS_NCHAR_CHARACTERSET'; |
35 | 50 |
|
36 | 51 | Setting the Client Character Set |
37 | | -================================ |
| 52 | +-------------------------------- |
38 | 53 |
|
39 | 54 | In python-oracledb, the encoding used for all character data is "UTF-8". The |
40 | 55 | ``encoding`` and ``nencoding`` parameters of the :meth:`oracledb.connect` |
41 | | -and :meth:`oracledb.create_pool` methods are ignored. |
| 56 | +and :meth:`oracledb.create_pool` methods are deprecated and ignored. |
42 | 57 |
|
43 | | -.. _findingcharset: |
44 | 58 |
|
| 59 | +Setting the Client Locale |
| 60 | +========================= |
45 | 61 |
|
46 | | -Finding the Oracle Database Character Set |
47 | | -========================================= |
| 62 | +Thick Mode Oracle Database National Language Support (NLS) |
| 63 | +---------------------------------------------------------- |
48 | 64 |
|
49 | | -To find the database character set, execute the query: |
| 65 | +The python-oracledb Thick mode uses Oracle Database's National Language Support |
| 66 | +(NLS) functionality to assist in globalizing applications, for example to |
| 67 | +convert numbers and dates to strings in the locale specific format. |
50 | 68 |
|
51 | | -.. code-block:: sql |
| 69 | +You can use the ``NLS_LANG`` environment variable to set the language and |
| 70 | +territory used by the Oracle Client libraries. For example, on Linux you could |
| 71 | +set:: |
52 | 72 |
|
53 | | - SELECT value AS db_charset |
54 | | - FROM nls_database_parameters |
55 | | - WHERE parameter = 'NLS_CHARACTERSET'; |
| 73 | + export NLS_LANG=JAPANESE_JAPAN |
56 | 74 |
|
57 | | -To find the database 'national character set' used for NCHAR and related types, |
58 | | -execute the query: |
| 75 | +The language ("JAPANESE" in this example) specifies conventions such as the |
| 76 | +language used for Oracle Database messages, sorting, day names, and month |
| 77 | +names. The territory ("JAPAN") specifies conventions such as the default date, |
| 78 | +monetary, and numeric formats. If the language is not specified, then the value |
| 79 | +defaults to AMERICAN. If the territory is not specified, then the value is |
| 80 | +derived from the language value. See `Choosing a Locale with the NLS_LANG |
| 81 | +Environment Variable |
| 82 | +<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-86A29834-AE29-4BA5-8A78-E19C168B690A>`__ |
59 | 83 |
|
60 | | -.. code-block:: sql |
| 84 | +If the ``NLS_LANG`` environment variable is set in the application with |
| 85 | +``os.environ['NLS_LANG']``, it must be set before any connection pool is |
| 86 | +created, or before any standalone connections are created. |
61 | 87 |
|
62 | | - SELECT value AS db_ncharset |
63 | | - FROM nls_database_parameters |
64 | | - WHERE parameter = 'NLS_NCHAR_CHARACTERSET'; |
| 88 | +Any client character set value in the ``NLS_LANG`` variable, for example |
| 89 | +``JAPANESE_JAPAN.JA16SJIS``, is ignored by python-oracledb. See `Setting the |
| 90 | +Client Character Set`_. |
65 | 91 |
|
66 | | -To find the current "client" character set used by python-oracledb, execute the |
67 | | -query: |
| 92 | +Other Oracle globalization variables, such as ``NLS_DATE_FORMAT`` can also be |
| 93 | +set to change the behavior of python-oracledb Thick, see `Setting NLS Parameters |
| 94 | +<https://www.oracle.com/pls/topic/lookup?ctx=dblatest& |
| 95 | +id=GUID-6475CA50-6476-4559-AD87-35D431276B20>`__. |
68 | 96 |
|
69 | | -.. code-block:: sql |
| 97 | +For more information, see the `Database Globalization Support Guide |
| 98 | +<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=NLSPG>`__. |
70 | 99 |
|
71 | | - SELECT DISTINCT client_charset AS client_charset |
72 | | - FROM v$session_connect_info |
73 | | - WHERE sid = SYS_CONTEXT('USERENV', 'SID'); |
| 100 | +.. _thindatenumber: |
74 | 101 |
|
| 102 | +Thin Mode Locale-aware Number and Date Conversions |
| 103 | +-------------------------------------------------- |
75 | 104 |
|
76 | | -.. _thindatenumber: |
| 105 | +.. note:: |
77 | 106 |
|
78 | | -Locale-aware Number and Date Conversions in python-oracledb Thin Mode |
79 | | -===================================================================== |
| 107 | + All NLS environment variables are ignored by the python-oracledb Thin mode. |
| 108 | + Also the ``ORA_SDTZ`` and ``ORA_TZFILE`` variables are ignored. |
80 | 109 |
|
81 | | -In python-oracledb Thick mode, Oracle NLS routines convert numbers and dates to |
82 | | -strings in the locale specific format. But in the python-oracledb Thin mode, |
83 | | -output type handlers need to be used to perform similar conversions. The |
84 | | -examples below show a simple conversion and also how the Python locale module |
85 | | -can be used. Type handlers like those below can also be used in |
86 | | -python-oracledb Thick mode. |
| 110 | +In the python-oracledb Thin mode, output type handlers need to be used to |
| 111 | +perform similar conversions. The examples below show a simple conversion and |
| 112 | +also how the Python locale module can be used. Type handlers like those below |
| 113 | +can also be used in python-oracledb Thick mode. |
87 | 114 |
|
88 | 115 | To convert numbers: |
89 | 116 |
|
@@ -188,33 +215,3 @@ To convert dates: |
188 | 215 | for row in cursor: |
189 | 216 | print(row) # gives 'Mi 15 Dez 19:57:56 2021' |
190 | 217 | print() |
191 | | -
|
192 | | -
|
193 | | -.. _thicklocale: |
194 | | - |
195 | | -Setting the Oracle Client Locale in python-oracledb Thick Mode |
196 | | -============================================================== |
197 | | - |
198 | | -You can use the ``NLS_LANG`` environment variable to set the language and |
199 | | -territory used by the Oracle Client libraries. For example, on Linux you could |
200 | | -set:: |
201 | | - |
202 | | - export NLS_LANG=JAPANESE_JAPAN |
203 | | - |
204 | | -The language ("JAPANESE" in this example) specifies conventions such as the |
205 | | -language used for Oracle Database messages, sorting, day names, and month |
206 | | -names. The territory ("JAPAN") specifies conventions such as the default date, |
207 | | -monetary, and numeric formats. If the language is not specified, then the value |
208 | | -defaults to AMERICAN. If the territory is not specified, then the value is |
209 | | -derived from the language value. See `Choosing a Locale with the NLS_LANG |
210 | | -Environment Variable |
211 | | -<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-86A29834-AE29-4BA5-8A78-E19C168B690A>`__ |
212 | | - |
213 | | -If the ``NLS_LANG`` environment variable is set in the application with |
214 | | -``os.environ['NLS_LANG']``, it must be set before any connection pool is |
215 | | -created, or before any standalone connections are created. |
216 | | - |
217 | | -Other Oracle globalization variables, such as ``NLS_DATE_FORMAT`` can also be |
218 | | -set to change the behavior of python-oracledb Thick, see `Setting NLS Parameters |
219 | | -<https://www.oracle.com/pls/topic/lookup?ctx=dblatest& |
220 | | -id=GUID-6475CA50-6476-4559-AD87-35D431276B20>`__. |
0 commit comments