我有这个代码:
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>,)]
但是,当我打印template.read()时,我得到这个错误:
cx_Oracle.DatabaseError: Invalid handle!
如何获取和读取这些数据?谢谢.
解决方法
我发现,如果在使用cx_Oracle.LOB.read()方法之前连接到Oracle是关闭的,则会发生这种情况.
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:无效句柄!
但以下代码的作用如下:
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