python中mysql的row_工厂替代方案
我在sqllite中使用了row_factory,这样就可以使用列名以字典格式返回我的行。我用过:python中mysql的row_工厂替代方案,python,mysql,sqlite,Python,Mysql,Sqlite,我在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] ret
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数据库中使用类似的东西,但我找不到替代方法,因为这个问题没有任何答案,我使用Barmar的评论解决了这个问题,我将其粘贴为可接受的解决方案
dictionary=True
参数到connection.cursor()
告诉它将行作为字典返回如果使用MySQL连接器,那么dictionary=True
参数到connection.cursor()
告诉它将行作为字典返回。