How do I read cx_Oracle.LOB data in Python?

前端 未结 4 588
庸人自扰
庸人自扰 2020-12-31 04:06

I have this code:

    dsn = cx_Oracle.makedsn(hostname, port, sid)
    orcl = cx_Oracle.connect(username + \'/\' + password + \'@\' + dsn)
    curs = orcl.cu         


        
相关标签:
4条回答
  • 2020-12-31 04:32

    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
    
    0 讨论(0)
  • 2020-12-31 04:35

    There should be an extra comma in the for loop, see in below code, i have supplied an extra comma after x in for loop.

    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()
    for x, in rows:
        print(x)
    
    0 讨论(0)
  • 2020-12-31 04:45

    You basically have to loop through the fetchall object

    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()
    for x in rows:
       list_ = list(x)
       print(list_)
    
    0 讨论(0)
  • 2020-12-31 04:46

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

    curs.execute(sql)        
    for row in curs:
        print row[0].read()
    
    0 讨论(0)
提交回复
热议问题