Good day! I use Python 3.5 to solve this problem + fdb library. 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 attempting to save the file, an error is generated:

 UnicodeDecodeError Traceback (most recent call last) <ipython-input-1-6a7f8cf5e256> in <module>() 6 cur = con.cursor() 7 cur.execute("select FILE_DATA from DOC_ATTACHMENTS where OID = '620104150893'") ----> 8 fileToSave= cur.fetchone()[0] 9 with open('c:\\python5.jpg', 'wb') as f: 10 f.write(fileToSave) C:\Program Files\Anaconda3\lib\site-packages\fdb\fbcore.py in fetchone(self) 3635 """ 3636 if self._ps: -> 3637 return self._ps._fetchone() 3638 else: 3639 raise ProgrammingError("Cannot fetch from this cursor because" C:\Program Files\Anaconda3\lib\site-packages\fdb\fbcore.py in _fetchone(self) 3315 ctypes.cast(ctypes.pointer(self._out_sqlda), XSQLDA_PTR)) 3316 if self._last_fetch_status == 0: -> 3317 return self.__XSQLDA2Tuple(self._out_sqlda) 3318 elif self._last_fetch_status == self.RESULT_SET_EXHAUSTED: 3319 self._free_handle() C:\Program Files\Anaconda3\lib\site-packages\fdb\fbcore.py in __XSQLDA2Tuple(self, xsqlda) 2735 if ((self.__charset or PYTHON_MAJOR_VER == 3) 2736 and sqlvar.sqlsubtype == 1): -> 2737 value = b2u(value,self.__python_charset) 2738 elif vartype == SQL_ARRAY: 2739 value = [] C:\Program Files\Anaconda3\lib\site-packages\fdb\fbcore.py in b2u(st, charset) 432 "Decode to unicode if charset is defined. For conversion of result set data." 433 if charset: --> 434 return st.decode(charset) 435 else: 436 return st C:\Program Files\Anaconda3\lib\encodings\cp1251.py in decode(self, input, errors) 13 14 def decode(self,input,errors='strict'): ---> 15 return codecs.charmap_decode(input,errors,decoding_table) 16 17 class IncrementalEncoder(codecs.IncrementalEncoder): UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 690: character maps ` 

Encoding of the field in the database: Win-1251 , file type: Blob .

  • What could be the jpg blob encoding? This is just an array of bytes. - Sergey
  • Why do you have code trying to decode binary data (such as jpeg) into text (Unicode)? The mistake to try to decode such data regardless of the encoding is not text. Do you get to write / read blob at all? For example, data = bytearray(range(0x100)) create a minimal but complete example that writes this data to the simplest table. - jfs

0