我在用
Python 2.7
cx_Oracle 6.0.2
我在我的代码中做这样的事情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
我得到了这样的输出Connection Version: 11.2.0.2.0
(1, u'????????????', 'test')
我正在使用以下查询在 oracle 数据库中创建表
CREATE TABLE product_information
( product_id NUMBER(6)
, product_name NVARCHAR2(100)
, product_description VARCHAR2(1000));
我使用以下查询插入数据insert into product_information values(2, 'दुःख', 'teting');
编辑 1 查询:
SELECT * from NLS_DATABASE_PARAMETERS WHERE parameter IN ( 'NLS_LANGUAGE', 'NLS_TERRITORY', 'NLS_CHARACTERSET');
结果NLS_LANGUAGE: AMERICAN, NLS_TERRITORY: AMERICA, NLS_CHARACTERSET: AL32UTF8
最佳答案
我解决了这个问题。
首先我加了NLS_LANG=.AL32UTF8
作为安装 Oracle 的系统中的环境变量
其次,我在 cx_Oracle 的连接函数中传递了 encoding 和 nencoding 参数,如下所示。
cx_Oracle.connect(username, password, connection_string,
encoding="UTF-8", nencoding="UTF-8")
此问题也在此处讨论 https://github.com/oracle/python-cx_Oracle/issues/157
关于python-2.7 - 如何处理 cx_Oracle 和 python 2.7 中的 unicode 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49174710/