You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Replit中MySQL数据展示时的‘cannot convert dictionary update...’错误

解决SQLAlchemy转Row为字典报错:cannot convert dictionary update sequence element #0 to a sequence

这个错误是因为直接用dict(row)转换SQLAlchemy的Row对象时,Row的迭代逻辑不符合dict()的预期——dict()需要接收键值对组成的序列(比如元组列表),但某些版本的SQLAlchemy Core中,Row对象直接迭代时只会返回列值,而非键值对,导致转换失败。

修复方案1:使用Row的_asdict()方法

SQLAlchemy的Row对象内置了_asdict()方法,可直接将行数据转为字典:

def load_jobss_from_db():
    with engine.connect() as conn:
        result = conn.execute(text("select * from jobs"))
        jobs = []
        for row in result.all():
            jobs.append(row._asdict())  # 替换dict(row)为row._asdict()
        return jobs

修复方案2:使用result.mappings()直接获取字典结果

更简洁的方式是用result.mappings(),它会返回字典格式的结果集,直接转换即可:

def load_jobss_from_db():
    with engine.connect() as conn:
        result = conn.execute(text("select * from jobs"))
        # 直接遍历mappings()结果并转为字典
        return [dict(row) for row in result.mappings()]

内容的提问来源于stack exchange,提问作者Dipanitha D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 03:22:51