"Fdb Exception: 'utf-8'编解码器无法解码字节"

3

我正在编写一个小的Python应用程序,从Firebird数据库中读取数据。我使用的是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和其他格式的解码和编码。
我很困惑。


那个字符串是从哪里来的?表面上看,该字符串不包含字节0xe8(例如在Windows-1252中,0xe8是è),因此这可能不是实际的字符串,或者您的问题出在其他地方。请提供足够的代码和信息以重现问题。另外,如果您的问题与该字符串有关,则这与FDB或Firebird 2.5嵌入式有什么关系呢? - Mark Rotteveel
1
@Arioch'The 这个问题似乎出现在连接数据库之前。 - Mark Rotteveel
@MarkRotteveel 您是正确的。感谢您的意见。 - user8794709
你找到解决方案了吗? - Geovani Ferreira
很抱歉,@GeovaniFerreira。我已经停止在该应用程序中使用UNC路径了。 - user8794709
显示剩余9条评论
1个回答

1
你需要在数据库连接中设置字符集。
例如,使用 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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接