1

我前段时间下载了 cx_oracle 并编写了一个脚本来将数据转换为 XML。我不得不重新安装我的操作系统并获取最新版本的 cx_Oracle (5.0.3),突然间我的代码被破坏了。

第一件事是 cx_Oracle.connect 想要 unicode 而不是字符串作为用户名和密码,这很容易修复。但是现在它在 cursor.execute 上一直失败并告诉我我的字符串不是字符串,即使 type() 告诉我它是一个字符串。

这是我多年前最初使用的测试脚本,在我的旧版本上运行良好,但现在不适用于 cx_Oracle。

    import cx_Oracle

ip = 'url.to.oracle'
port = 1521
SID = 'mysid'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)

connection = cx_Oracle.connect(u'name', u'pass', dsn_tns)
cursor = connection.cursor()
cursor.arraysize = 50

sql = "select isbn, title_code from core_isbn where rownum<=20"
print type(sql)
cursor.execute(sql)

for isbn, title_code in cursor.fetchall():
    print "Values from DB:", isbn, title_code

cursor.close()
connection.close()

当我运行时,我得到:

Traceback(最近一次调用最后一次):文件“C:\NetBeansProjects\Python\src\db_temp.py”,第 48 行,在 cursor.execute(sql) 类型错误:期待 None 或字符串

有谁知道我可能做错了什么?

4

2 回答 2

4

我看到他们现在支持 python 3。可能是他们想要一个unicode对象而不是str,并且他们在该错误消息中使用了 python 3 术语,即使他们可能没有在与用户/密码相关的错误消息中。

于 2010-05-25T16:38:22.760 回答
1

也许更改您的编码设置会有所帮助。

我有这个:

- - 编码:iso-8859-1 - -

你可能有:

-- 编码:utf-8 --

于 2011-01-27T15:16:23.307 回答