python - <sqlite3.Row 对象在 0x1017fe3f0> 而不是数据库内容

标签 python sqlite flask

我的模板是这样的:

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

我的路由功能是这样的:
@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)

知道为什么我要获取行对象位置而不是数据库内容吗?

最佳答案

您需要从行中获取数据:

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

SQL 查询总是按行返回数据,其中包含列。以上假设您的SELECT每行仅返回一列。

如果您有多个列,则必须使用直接索引( order[1]order[3] 等)来解决这些问题,或者在行上循环以显示列:
{% for order in orders %}
  <li><h2>{{ order[0] }}</h2>
      {% for column in order %}
          {% if not loop.first %}{{ column }}<br />{% endif %}
      {% endfor %}
  </li>
{% endfor %}

关于python - <sqlite3.Row 对象在 0x1017fe3f0> 而不是数据库内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23857386/

相关文章:

python - 如何在python flask(restplus)中的GET请求中将URL作为参数传递?

python - 如何在较大的 Python/Flask 应用程序中组织数据库连接?

Python - 使用 open() 除了 (FileNotFoundError)?

mysql - 我想选择批处理 ID,其中包含从 SQL 表中提供的两个选定类(class)

SQLite - 根据表b中相关行的计算结果从表a中选择

javascript - 为什么我的 Flask-SocketIO 服务器没有成功地向我的客户端 JavaScript 发送消息?

Python LDIF 解析器

python - 如何禁用 Matplotlib 中的键盘快捷键?

Python 3.9 - 安装不正确,无法导入 SSL

python - "Execute Python Script"模块 :Common table expressions is not supported in sqlite3 中的 Azure ML