如何在cx_Oracle和python 2.7中处理unicode数据? [英] How to handle unicode data in cx_Oracle and python 2.7?
问题描述
我正在使用
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
我得到了这样的输出
连接版本:11.2.0.2.0
Connection Version: 11.2.0.2.0
(1,u'????????????','test')
(1, u'????????????', 'test')
我正在使用以下查询在oracle数据库中创建表
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');
编辑1
查询:SELECT * from NLS_DATABASE_PARAMETERS WHERE parameter IN ( 'NLS_LANGUAGE', 'NLS_TERRITORY', 'NLS_CHARACTERSET');
结果
NLS_LANGUAGE:美国,NLS_TERRITORY:美国,NLS_CHARACTERSET: AL32UTF8
NLS_LANGUAGE: AMERICAN, NLS_TERRITORY: AMERICA, NLS_CHARACTERSET: AL32UTF8
推荐答案
我解决了这个问题.
首先,我在安装Oracle的系统中添加了NLS_LANG=.AL32UTF8
作为环境变量.
其次,我在cx_Oracle的connect函数中传递了encoding和nencoding参数,如下所示.
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")
此问题也在 https://github.com/oracle/python进行了讨论-cx_Oracle/issues/157
这篇关于如何在cx_Oracle和python 2.7中处理unicode数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!