python - 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)
解决方案
我有同样的问题。
在我的情况下,数据库不是 UTF-8。在连接字符串中设置正确的字符集后,它起作用了: ?charset=ISO8859_1
推荐阅读
- css - 我应该如何使我身体的整个背景变暗?
- docker - Filebeat 不会将日志发送到 ELK docker stack 上的 logstack
- wordpress - 挂钩添加到购物车方法以执行自定义功能之前
- ruby-on-rails - 基于活动记录数据库查询更改类,在rails循环中使用link_to?
- sql-server - 在 SQL Server 中将 DT_DBTIMESTAMP2 类型的列映射到 datetime2 时发生强制转换规范错误的无效字符值
- mysql - 大多数共享主机上是否禁用了 LOAD DATA INFILE?
- excel - 计算单元格列中的行数
- python - 转换为 Logn Python 3.7
- java - TextSwitcher 的自动调整大小 TextView
- c++ - 用cl.exe和/link.exe构建的Vscode C++调试代码——调试器不附加