How to get pdf/jpg files from Firebird 2.5 via Python 3.5?

1.1k Views Asked by At

I use Python 3.5 for this task + library fdb. My script:

import fdb
con = fdb.connect(
    host='host', database='database',
    user='IAKUZNETSOV', password='111111'
  )
cur = con.cursor()
cur.execute("select DATA from ATTACHMENTS where OID = '6512165313'")
fileToSave= cur.fetchone()[0]
with open('c:\\python5.jpg', 'wb') as f:
    f.write(fileToSave)

After attempts to save the file I receive error:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 578: character maps to <undefined>

Encoding fields in the database: Win-1251 type: Blob.

How can I fix it?

1

There are 1 best solutions below

0
Val Marinov On

This error occurs because the subtype 1(text) of blob field.

The subtypes are:

0 - binary data (image, video, audio, whatever)

1 - text (basiccharacter functions work)

2 - BLR (used for definitions of Firebird procedures, triggers, etc.)

User applications should only use subtypes 0 and 1.

If you can not change subtype to 0, you may try to convert the data into raw bytes into client application.