Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blob data from Oracle to text file using python

I have been trying to get the blob data from oracle into a text file using Python. I couldn't find the answer on any of the other links.

Below is my code :

sql_string = """select 
   event_id
   ,blob_length
   ,blob field
from table"""

  cur.execute(sql_string)
    path = "P:/Folders/"

    for row in cur:
        filename = path +  "notes_" + str(row[0]) + "_" + str(row[1]) + ".txt"      
        f = codecs.open(filename, encoding='utf-8', mode='wb+')
        f.write(row[2])
        f.close()

I get the below error

TypeError: utf_8_encode() argument 1 must be str, not cx_Oracle.LOB

I have tried a few other ways but the problem is that even other approaches that I've seen only handle strings and not blobs.

like image 632
Doodle Avatar asked Feb 06 '26 20:02

Doodle


1 Answers

You have to use the cx_oracle.LOB.read() method to get the content of the LOB object:

f.write(row[2].read())
like image 113
blhsing Avatar answered Feb 09 '26 12:02

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!