【发布时间】: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 中使用类似的东西,但我找不到替代方法
【问题讨论】:
-
如果你使用 MySQL 连接器,
connection.cursor()的dictionary=True参数告诉它以字典的形式返回行。