0

I would like to create a file from octet-stream data.

From a OracleDB I get the following data:

b'_RCFM*=.u0x\x9c\xec\xbb\x05\\\x94M\xdb7\xbc,\x0b,\x8dtI\x08\x82\xa0\x94J\xc7.(\xa14H7HKH\xf7\x12""\ ... \xff\x05o\xe2x2'

My current python script looks like this:

import oracledb

oracledb.init_oracle_client(lib_dir=r"C:\instantclient_21_6")
dsn_tns = oracledb.makedsn('xxx', 3333, service_name='XXX') 
conn = oracledb.connect(user='xxx', password='xxx', dsn=dsn_tns) 

def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.DB_TYPE_CLOB:
        return cursor.var(oracledb.DB_TYPE_LONG_RAW, arraysize=cursor.arraysize)
    if default_type == oracledb.DB_TYPE_BLOB:
        return cursor.var(oracledb.DB_TYPE_LONG_RAW, arraysize=cursor.arraysize)
    else:
        return cursor.var(oracledb.DB_TYPE_LONG_RAW, arraysize=cursor.arraysize)

conn.outputtypehandler = output_type_handler

c = conn.cursor()
c.execute(''' SELECT "DATA"
FROM "ZITSMSCHUL" . "SYSATTACHMEM1" 
WHERE "TOPIC" ='IM28588' ''') 

res = c.fetchall()

strRes = str(res)

strRes = strRes[2:-3]

filePath = 'C:/Users/123/Desktop/37505_ping.doc'
with open(filePath, 'w+b') as f: 
    f.write(bytearray(strRes,encoding='utf-8'))
    f.close()

c.close()
conn.close() 

This code gives me a .doc file but with the octet stream as content and not the original data.

I'm thankful for any help

2
  • Probably a duplicate of stackoverflow.com/questions/51868112/… Commented Oct 10, 2022 at 12:30
  • 1
    To start with, your type handler return type for the CLOB case looks 'wrong', see the example in python-oracledb.readthedocs.io/en/latest/user_guide/… which returns DB_TYPE_LONG for this case. But since you're using python-oracledb, you can simply set the new attribute oracledb.defaults.fetch_lobs = False and remove the handler. However hopefully you are storing the binary in a BLOB, so the issue may be on the Python side - there are plenty of Stack Overflow posts about writing to files you can review. Commented Oct 10, 2022 at 21:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.