python-2.7 - 如何处理 cx_Oracle 和 python 2.7 中的 unicode 数据?

标签 python-2.7 unicode oracle11g cx-oracle

我在用

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/

相关文章:

python - django-social-auth - 缺少 HTTPSConnection

检查是否显示Unicode字符或tofu

java - 如何使用 Java 从 MySQL 数据库中读取保加利亚语字符?

java - 使用java和jdbc可以写入但无法从MYSQL表中读取Unicode

sql - 为什么这个触发器不好?

oracle - 如何知道数据库连接数

python - 无法将数组数据从 dtype ('O' ) 转换为 dtype ('float64' )

python - 生成 CSV 和空行

database - 如何在oracle中使用内壳?

python - 如何区分python脚本是在putty ssh上运行还是在VNC终端上运行?