我前段时间下载了 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 或字符串
有谁知道我可能做错了什么?