【问题标题】:retrieve data from database into html using flask使用烧瓶将数据从数据库检索到 html
【发布时间】:2020-02-29 16:35:23
【问题描述】:

我想在单击搜索按钮时在 HTML 网页中显示来自 PostgreSQL 数据库的数据。当我搜索一行的值时,我得到了所有行的值。我想显示该特定行的值,而不是所有行的值。

@app.route("/search", methods=['GET','POST'])
def search():
    if request.method == "POST":  
        course = request.form['book']
        # input_id = request.POST["book"]
        # cur = conn.cursor()
        try:
            cursor.execute("select course_id,course_name  from course where course_id < 5;",(course))
        except Exception,e:
            conn.rollback()
        else:
            conn.commit()
        data = cursor.fetchall()
        # if len(data) == 0 and course == 5:
        #     cursor.execute("select course_id,course_name from course;",(course_id,))
        #     conn.commit()
        #     data = cursor.query.all()

        return render_template('search.html', data=data)
    return render_template('search.html')
if __name__ == "__main__":
 from flask_sqlalchemy import get_debug_queries
 app.run(debug=True)

    enter code here

【问题讨论】:

    标签: javascript python postgresql flask


    【解决方案1】:

    您只是选择 course_id

    cursor.execute("select course_id,course_name  from course where course_id < 5;",(course))
    

    尝试将该行更改为:

    cursor.execute("select course_id,course_name  from course where course_id = %s",(course,))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2020-10-27
      • 2021-11-04
      • 2021-06-15
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      相关资源
      最近更新 更多