Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 356
metersk Avatar asked May 25 '14 16:05

metersk


1 Answers

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 %}
like image 75
Martijn Pieters Avatar answered Oct 25 '22 00:10

Martijn Pieters