【问题标题】:row_factory alternative for mysql in pythonpython中mysql的row_factory替代品
【发布时间】:2019-11-20 23:36:25
【问题描述】:

我在 sqllite 中使用了 row_factory,以便我的行可以以带有列名的字典格式返回。我用过:

conn = sqlite3.connect('gutendex.db')
conn.row_factory = dict_factory

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

我也想在 mysql db 中使用类似的东西,但我找不到替代方法

【问题讨论】:

标签: python mysql sqlite


【解决方案1】:

由于这个问题没有任何答案,我使用 Barmar 的评论解决了这个问题,我将其粘贴为已接受的解决方案。

connection.cursor()dictionary=True 参数告诉它以字典的形式返回行

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2010-09-24
    • 1970-01-01
    • 2015-01-19
    • 2018-08-27
    • 2021-02-11
    相关资源
    最近更新 更多