How do I read cx_Oracle.LOB data in Python?

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I have this code:

    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() 

When I do print rows, I get this:

[(<cx_Oracle.LOB object at 0x0000000001D49990>,)] 

However, when I do print template.read(), I get this error:

cx_Oracle.DatabaseError: Invalid handle!

Do how do I get and read this data? Thanks.

回答1:

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() 

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 


回答2:

Figured it out. I have to do something like this:

curs.execute(sql)         for row in curs:     print row[0].read() 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!