首页 > 解决方案 > SqlAlchemy + Firebird + FDB 的 UnicodeError

问题描述

我正在尝试显示来自 firebird 3.x 数据库的结果,但得到:

文件“/...../Envs/pos/lib/python3.6/site-packages/fdb/fbcore.py”,第 479 行,在 b2u 返回 st.decode(charset) UnicodeDecodeError: 'utf-8' codec无法解码位置 9 中的字节 0xd1:无效的继续字节

尽管我到处都设置了 utf-8:

# -- coding: UTF-8 -- 

import os

os.environ["PYTHONIOENCODING"] = "utf8"

from sqlalchemy import *

SERVIDOR = "localhost"
BASEDATOS_1 = "db.fdb"

PARAMS = dict(
    user="SYSDBA",
    pwd="masterkey",
    host="localhost",
    port=3050,
    path=BASEDATOS_1,
    charset='utf-8'
)

firebird = create_engine("firebird+fdb://%(user)s:%(pwd)s@%(host)s:%(port)d/%(path)s?charset=%(charset)s" % PARAMS, encoding=PARAMS['charset'])

def select(eng, sql):
    with eng.connect() as con:
        return eng.execute(sql)

for row in select(firebird, "SELECT * from clientes"):
    print(row)

标签: pythonpython-3.xunicodesqlalchemy

解决方案


我有同样的问题。
在我的情况下,数据库不是 UTF-8。在连接字符串中设置正确的字符集后,它起作用了: ?charset=ISO8859_1


推荐阅读