FirebirdSQL / fdb

Firebird Driver for Python
https://www.firebirdsql.org/en/devel-python-driver/
Other
60 stars 26 forks source link

UnicodeDecodeError on database connection #19

Closed neo-oien closed 3 years ago

neo-oien commented 3 years ago

Hello!

I try to connect to the .fdb database with 'Default Character Set' as 'WIN1252' on a remote Windows server (database version is 11).

I use the next command (fdb + Python3):

fdb.connect(dsn='host/3050:path/database.FDB', user='user', password='password', charset='WIN1252', sql_dialect=3)

Then, calling I get this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 68: invalid start byte

I have tried setting the encoding as 'NONE', None mentioned in #12

Thanks!

pcisar commented 3 years ago

Could you please post a stack trace for this exception?

neo-oien commented 3 years ago
Traceback (most recent call last):
  File "/opt/my_project_directory/models/res_config_settings.py", line 43, in run_connector
    import traceback
  File "/home/user/.local/share/virtualenvs/my_project-vN1VkJiM/lib/python3.6/site-packages/fdb/fbcore.py", line 867, in connect
    "Error while connecting to database:")
  File "/home/user/.local/share/virtualenvs/my_project-vN1VkJiM/lib/python3.6/site-packages/fdb/fbcore.py", line 604, in exception_from_status
    msglist.append('- ' + (msg.value).decode(sys_encoding))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 68: invalid start byte
pcisar commented 3 years ago

Ok, the problem is actually caused by error message sent by Firebird server that is not in sys_encoding (surprise, surprise, it's from another system which may have other one). So this exception actually masks the real problem. Firebird messages are typically in ascii (because they are in english) which plays nicely with utf-8. But if the message contains information from host system (like paths and OS messages), they could contain any weird stuff if it's localised OS. Problem is, that there is no good solution. You may avoid this exception to get to the Firebird error message by changing the decode() call in fbcore.py", line 604, in exception_from_status to use errors='replace' parameter.

neo-oien commented 3 years ago

Thanks so much! It has worked. In fact, it was an error from the database path that the server returned to me in a different encoding from my system and from that of the database. Best regards