Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle unicode data in cx_Oracle and python 2.7?

I am using

Python 2.7
cx_Oracle 6.0.2

I am doing something like this in my code

import cx_Oracle
connection_string = "%s:%s/%s" % ("192.168.8.168", "1521", "xe")
connection = cx_Oracle.connect("system", "oracle", connection_string)
cur = connection.cursor()
print "Connection Version: {}".format(connection.version)
query = "select *from product_information"
cur.execute(query)
result = cur.fetchone()
print result

I got the output like this

Connection Version: 11.2.0.2.0

(1, u'????????????', 'test')

I am using following query to create table in oracle database

CREATE TABLE product_information 
    ( product_id          NUMBER(6) 
    , product_name        NVARCHAR2(100) 
    , product_description VARCHAR2(1000));

I used the following query to insert data

insert into product_information values(2, 'दुःख', 'teting');

Edit 1

Query: SELECT * from NLS_DATABASE_PARAMETERS WHERE parameter IN ( 'NLS_LANGUAGE', 'NLS_TERRITORY', 'NLS_CHARACTERSET');

Result

NLS_LANGUAGE: AMERICAN, NLS_TERRITORY: AMERICA, NLS_CHARACTERSET: AL32UTF8

like image 823
Sachin Aryal Avatar asked Dec 23 '22 09:12

Sachin Aryal


1 Answers

I solved the problem.

First I added NLS_LANG=.AL32UTF8 as the environment variable in the system where Oracle is installed Second I passed the encoding and nencoding parameter in connect function of cx_Oracle like below.

cx_Oracle.connect(username, password, connection_string,
                                            encoding="UTF-8", nencoding="UTF-8")

This issue is also discussed here at https://github.com/oracle/python-cx_Oracle/issues/157

like image 170
Sachin Aryal Avatar answered Feb 01 '23 20:02

Sachin Aryal