<sqlite3.Row object at 0x1017fe3f0> instead of database contents

拈花ヽ惹草 提交于 2021-02-08 13:21:14

问题


My templates is this:

  {% for order in orders %}
    <li><h2>{{ order}}</h2>
  {% endfor %}

and my routing function is this:

@app.route('/order_history')
def orderhistory():
    db = get_db()
    cur = db.execute('select * from order_items')
    orders = cur.fetchall()
    return render_template('order_history.html', orders = orders)

Any idea why I am getting row object locations instead of the db contents?


回答1:


You need to get the data from the rows:

{% for order in orders %}
  <li><h2>{{ order[0] }}</h2></li>
{% endfor %}

A SQL query always returns data in rows, containing columns. The above assumes your SELECT returned just one column per row.

If you have multiple columns, you'd have to either address these with direct indices (order[1], order[3], etc.) or loop over the row to show the columns:

{% for order in orders %}
  <li><h2>{{ order[0] }}</h2>
      {% for column in order %}
          {% if not loop.first %}{{ column }}<br />{% endif %}
      {% endfor %}
  </li>
{% endfor %}


来源:https://stackoverflow.com/questions/23857386/sqlite3-row-object-at-0x1017fe3f0-instead-of-database-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!