cx.oracle12705,无法使用cx插入Unicode

我在将unicode插入Oracle模式时遇到问题,我认为数据库是Oracle 11g实例,但目前还不能确定。我在OSX10.6.8上使用的是Python2.6.1(这是python的系统版本),使用的是从sourceforge.net下载的cx Oracle驱动程序模块版本5.1,构建并安装到一个Virtualenv1.6.1实例中,可以看到站点包。我的剧本如下import cx_Oracle

connection = cx_Oracle.connect(

"/@/"

)

cursor = connection.cursor()

result = cursor.execute(u"create table UNICODE_TEST (id NUMBER(6), text NCLOB not NULL)")

raw_text = open("test.txt",'r').read()

if isinstance(raw_text,str):

raw_text = raw_text.decode("utf_8")

statement = u"insert into UNICODE_TEST (id, text) values (1,'%s')" % raw_text

result = cursor.execute(statement)

我创建一个连接,创建光标,执行statent来创建一个id和文本字段类型为NUMBER和nclb的测试表。

我打开一个文件,其中包含我知道的用UTF-8编码的文本,将字符串解码为unicode。

在unicode字符串中创建insert语句并执行该语句,结果就是出现此错误。Traceback (most recent call last):

File "unicode-test.py", line 19, in

result = cursor.execute(statement)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 170: ordinal not in range(128)

在将语句插入Oracle模式之前,有人试图将其编码为ASCII。因此,我开始四处寻找,以更好地了解cx Oracle如何处理unicode,并在我从sourceforge.net下载的cx Oracle源文件HISTORY.txt中找到了这一点Changes from 5.0.4 to 5.1

1) Remove support for UNICODE mode and

permit Unicode to be passed through in

everywhere a string may be passed in. This means that strings will be

passed through to Oracle using the value of the NLS_LANG environment

variable in Python 3.x as well. Doing this eliminated a bunch of problems

that were discovered by using UNICODE mode and also removed an unnecessary

restriction in Python 2.x that Unicode could not be used in connect strings

or SQL statements, for example.

...

我的假设是,NLS_LANG环境变量被设置为'ascii'或其他等效值,因此我尝试将NLS_LANG设置为'AL32UTF8',我认为这是unicode的正确值,并在创建连接之前设置新值。os.environ["NLS_LANG"] = "AL32UTF8"

connection = cx_Oracle.connect(

"/@/"

)

cursor = connection.cursor()

...

但我知道这个错误。Traceback (most recent call last):

File "unicode-test.py", line 11, in

"/@/"

cx_Oracle.DatabaseError: ORA-12705: Cannot access NLS data files or invalid environment specified

所以看起来我不能修改NLS_LANG值。

这是我现在的问题。我是否遗漏了一些简单的内容,比如不正确的列类型?cx Oracle驱动程序有问题吗?在构建cx Oracle模块时,是否需要设置“WITH_UNICODE”环境变量?我将如何进行设置?Oracle实例有问题吗?我对甲骨文几乎没有经验,也从未与甲骨文和python一起工作过。我花了两天时间研究这个问题,希望在和DBA小组一起去之前能更好地理解这个问题。

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值