Python 无法从flask应用程序中的mysql.connector获取dict
我正在将flask应用程序从SQLite移植到MySQL。我的app.py中有以下内容:Python 无法从flask应用程序中的mysql.connector获取dict,python,mysql,flask,Python,Mysql,Flask,我正在将flask应用程序从SQLite移植到MySQL。我的app.py中有以下内容: def query_db(query, args=(), one=False): con = get_db() cur = con.cursor() cur.execute(query, args) result = cur.fetchall() cur.close() return (result[0] if result else None) if
def query_db(query, args=(), one=False):
con = get_db()
cur = con.cursor()
cur.execute(query, args)
result = cur.fetchall()
cur.close()
return (result[0] if result else None) if one else result
@app.route("/")
def index():
rows = query_db("SELECT * FROM computers")
return render_template("index.html", rows = rows)
在控制台中,我得到:
('FE:CB:40:50:B9:04', '192.168.11.163', 'Jules - Desktop (iMac)', None, 0, Decimal('0'), Decimal('0'), Decimal('0'), 0, 0, 0, 0, 0, 0, 0, 0, None, None, None, 1, 0, 0, None, None, 0, 0, '', 1, 1571097594)
在我的模板中,我有:
{% for row in rows %}
<tr>
<td>{{row['reboots']}}</td>
</tr>
{% endfor %}
…其中,
重新启动
是数据库中的一个字段。在迁移到MySQL之前,我没有更改模式,因此我希望模板能够按原样工作。我需要将结果转换为dict或其他什么吗?“我认为我根本没有得到元组”你为什么会这样想?很明显,您得到了一个元组(实际上是一个列表或元组的元组)。你犯的错误清楚地表明你是对的。它确实清楚地表明我得到了一个元组。尽管如此,为什么mysql.connector会为mysql返回不同的结果?我将为其他上下文添加一些模板代码。
`jinja2.exceptions.UndefinedError: 'tuple object' has no attribute 'reboots'`