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
DB_TYPE_LONGfor this case. But since you're using python-oracledb, you can simply set the new attributeoracledb.defaults.fetch_lobs = Falseand 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.