我正在编写一个从Firebird数据库读取数据的小型Python应用程序,使用的是fdb 1.8和Firebird Embedded 2.5,但无法解码以下字符串:

//centrale/Danea Easyfatt/ANYMA 2017 dal 06-02-17.eft


我不断收到此异常:

Traceback (most recent call last):
      File "C:\Matteo\PyCharm\CMakeR\God.py", line 165, in openDBFromMenu
        self.openDB(False)
      File "C:\Matteo\PyCharm\CMakeR\God.py", line 147, in openDB
        while (self.dbManager.connectTo(path)==False):
      File "C:\Matteo\PyCharm\CMakeR\DBManager.py", line 36, in connectTo
        charset="WIN1252"
      File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\fdb\fbcore.py", line 736, in connect
        "Error while connecting to database:")
      File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\fdb\fbcore.py", line 562, in exception_from_status
        msglist.append('- ' + (msg.value).decode('utf_8'))
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 38: invalid continuation byte


我该怎么办?我已经尝试过以WIN1252和其他格式进行解码和编码。

最佳答案

您需要在连接数据库时设置字符集。

例如,使用fdb包,您需要设置charset参数:

import fdb
con = fdb.connect(
dsn='bison:/temp/test.db',
user='sysdba', password='pass',
charset='WIN1251' # specify a correct character set for the connection




您可以使用sqlalchemy在database_uri中进行设置:

from sqlalchemy import create_engine
firebird = create_engine("firebird+fdb://%(user)s:%(pwd)s@%(host)s:%(port)/%(path)s?charset=%(charset)s"

关于python - fdb异常:'utf-8'编解码器无法解码字节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49283396/

10-14 08:09