简体   繁体   English

如何将结果从 SQLite db 显示到 html?

[英]How to display the result from SQLite db to html?

I am currently learning to build a small web-app.我目前正在学习构建一个小型网络应用程序。 I want to show the list of employees in the web page, and later the ability to add a new employee data to the database.我想在网页中显示员工列表,以及稍后将新员工数据添加到数据库的能力。 I am using SQlite db and Python Flask.我正在使用 SQlite db 和 Python Flask。 But I am stuck at this error message: UnboundLocalError: local variable 'rows' referenced before assignment .但我被这个错误消息困住了: UnboundLocalError: local variable 'rows' referenced before assignment So my question is, how do I fetch the content of my SQlite db and show them on my webpage?所以我的问题是,如何获取我的 SQlite 数据库的内容并将它们显示在我的网页上? I'd like to show it as a table as well.我也想将它显示为表格。

Here's my HTML code:这是我的 HTML 代码:

{% extends "layout.html" %}


{% block body %}
    <h1> Employees </h1>
    <ul>
        {% for employee in employees %}
        <li> {{ employee }}</li>
        {% endfor %}
    </ul>

    <a href = "/add">Add a new employee</a>
{% endblock %}

And my python code:还有我的python代码:

@app.route("/")
def tasks():
    rows = db.execute("SELECT * FROM employees")
    return render_template("employee_list.html", rows= rows) 

Anybody can help me?任何人都可以帮助我吗? Thank you.谢谢你。

use macro使用宏

{% macro render_comment(emp) %}
   <li>{{ emp }}</li>
{% endmacro %}
<ul>
     {% for emp in employees %}
          {{ render_comment(emp) }}
    {% endfor %}
</ul>

and change和改变

return render_template("employee_list.html", rows= rows)

to

return render_template("employee_list.html", employees= rows)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask:如何在 html 模板之间路由并显示来自 sqlite db 的结果? - Flask: How to route between html templates and display results from sqlite db? 如何从 sqlite3.db 中的 select 比较具有来自数据库的变量的方程式的结果? - how to select from sqlite3.db where i compare a result of equation that has variables from the db? 如何在 html django 页面中显示我的 sqlite 查询的结果 - How can I display the result of my sqlite query in a html django page 如何在从db获取的网页上显示html代码? - how to display the html code on a web page that is fetched from the db? Kivy / Python-如何在RecycleView的Popup中显示来自sqlite db的结果 - Kivy/Python - How to display results from sqlite db in Popup from RecycleView Flask:将数据从子页面保存到db后,如何使用首页上SQLite的数据更新HTML表? - Flask: How to update HTML table with data from SQLite on homepage after data are saved into db from subpage? 在 SQLite 中,如何使用输入框中的文本搜索我的数据库,并将结果显示为文本? - In SQLite, how do I search my database with text from an entry box, and display the result as text? 如何在数据库中存储HTML(SQLITE PYTHON) - How can I store HTML in a DB (SQLITE PYTHON) 根据列下拉列表在HTML页面中动态显示sqlite db的内容 - Dynamically display contents of sqlite db in a HTML page based on column pull downs 如何解码Mongo数据库中的芹菜结果? - How to decode celery result from the Mongo DB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM