0

i'm using cx_Oracle to access mon Oracle database i've been looking for a while a way to extract files from my database but i can't find the solution to it. And by extracting i mean download. Here's what i got so far :

from types import prepare_class
from typing import overload
import cx_Oracle

dsn_tns = cx_Oracle.makedsn('****', '****', service_name='****')
conn = cx_Oracle.connect(user=r'****', password='****', dsn=****)

c = conn.cursor()
c.execute('my request')

for row in c:
    print (row[0], '-', row[1])
conn.close()

i don't know if i need to use an external library or directly with a request.

Tsirsuna
  • 130
  • 2
  • 16
  • 1
    Do you mean that you're trying to query all of the rows in a database table and save them to a file? What kind of file - CSV? There's [several options in cx_oracle](https://oracle.github.io/python-cx_Oracle/samples/tutorial/Python-and-Oracle-Database-Scripting-for-the-Future.html#fetching) for fetching 1 row, multiple rows, or all the rows. You can also use a library like [pandas](https://stackoverflow.com/questions/35781580/cx-oracle-import-data-from-oracle-to-pandas-dataframe) – kfinity Nov 18 '21 at 14:29
  • ... what do you mean, specifically ? You just need to write the content into a file. – Maurice Meyer Nov 18 '21 at 14:31
  • there's blob files that i want to extract/download but yeah i could just open them one by one and write them in files but i don't know if it's quicker and i didn't knew cx_oracle could do it i'll look at how it works thanks – Tsirsuna Nov 18 '21 at 14:40
  • 1
    If the binary files stored as blob are text files, you can use a function to convert the blob to a clob, then download the clob as text in a normal extraction. The problem comes if the blob files are something different, like pictures or audio – Roberto Hernandez Nov 18 '21 at 15:43
  • 1
    Review the cx_Oracle doc https://cx-oracle.readthedocs.io/en/latest/user_guide/tuning.html and https://cx-oracle.readthedocs.io/en/latest/user_guide/lob_data.html – Christopher Jones Nov 18 '21 at 22:15

1 Answers1

1

If you have a column with the name and one with file contents you do something like :

for fileName,fileContents in c.execute('select fileName,file contents from the_table'):
    with open(fileName, w) as f:
        F.write(fileContents)
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
  • i see but when i open the file within the table F.write will go through the entire file and write it in another ? – Tsirsuna Nov 18 '21 at 14:50
  • 1
    I assume in your table you have the full file contents that what to be written in a file. So only one f.write – Ptit Xav Nov 18 '21 at 14:58