我可以使用 sqlalchemy 在 RowProxy 中分配值吗? [英] Can I assign values in RowProxy using the sqlalchemy?
本文介绍了我可以使用 sqlalchemy 在 RowProxy 中分配值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我想在网页中显示一些数据时,数据需要化妆,不知道如何实现,代码如下:
When I want to display some data in the web, the data need makeup, and I don't know how to achieve, here is the code:
from sqlalchemy import create_engine
engine = create_engine('mysql://root:111@localhost/test?charset=utf8')
conn = engine.connect()
articles = conn.execute('SELECT * FROM article')
articles = articles.fetchall()
for r in articles:
r['Tags'] = r['Keywords']
提示:'RowProxy'对象不支持item赋值.
It tips that: 'RowProxy' object does not support item assignment.
我该怎么做?
文章"表包含关键字"列,不包含标签"列.
The table 'article' contains the column 'Keywords', and not contains the column 'Tags'.
推荐答案
你可以从你的 RowProxy 中创建一个 dict,这将支持项目分配.
You can make a dict out of your RowProxy, which would support item assignment.
例如:
result_proxy = query.fetchall()
for row in result_proxy:
d = dict(row.items())
d['Tags'] = d['Keywords']
这篇关于我可以使用 sqlalchemy 在 RowProxy 中分配值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文