如何在Python中读取cx_Oracle.LOB数据? [英] How do I read cx_Oracle.LOB data in Python?
本文介绍了如何在Python中读取cx_Oracle.LOB数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
dsn = cx_Oracle.makedsn(hostname, port, sid)
orcl = cx_Oracle.connect(username + '/' + password + '@' + dsn)
curs = orcl.cursor()
sql = "select TEMPLATE from my_table where id ='6'"
curs.execute(sql)
rows = curs.fetchall()
print rows
template = rows[0][0]
orcl.close()
print template.read()
我做打印行
,我得到这个:
[(<cx_Oracle.LOB object at 0x0000000001D49990>,)]
但是,当我执行 print template.read()
时,我收到此错误:
However, when I do print template.read()
, I get this error:
cx_Oracle.DatabaseError:无效的句柄!
cx_Oracle.DatabaseError: Invalid handle!
如何获取和读取此数据?谢谢。
Do how do I get and read this data? Thanks.
推荐答案
我发现,在cx_Oracle.LOB.read之前关闭与Oracle的连接的情况下,会发生这种情况。
I've found out that this happens in case when connection to Oracle is closed before the cx_Oracle.LOB.read() method is used.
orcl = cx_Oracle.connect(usrpass+'@'+dbase)
c = orcl.cursor()
c.execute(sq)
dane = c.fetchall()
orcl.close() # before reading LOB to str
wkt = dane[0][0].read()
我得到: DatabaseError:无效的句柄!
但是以下代码有效:
And I get: DatabaseError: Invalid handle!
But the following code works:
orcl = cx_Oracle.connect(usrpass+'@'+dbase)
c = orcl.cursor()
c.execute(sq)
dane = c.fetchall()
wkt = dane[0][0].read()
orcl.close() # after reading LOB to str
这篇关于如何在Python中读取cx_Oracle.LOB数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文