Javascript 使用flask将数据库中的数据检索到html中

Javascript 使用flask将数据库中的数据检索到html中,javascript,python,postgresql,flask,Javascript,Python,Postgresql,Flask,我想通过单击搜索按钮在HTML网页中显示PostgreSQL数据库中的数据。当我搜索一行的值时,我得到的是所有行的值。我希望显示该特定行的值,而不是所有行的值 @app.route("/search", methods=['GET','POST']) def search(): if request.method == "POST": course = request.form['book'] # input_id = request.POST["bo

我想通过单击搜索按钮在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


您只是针对课程id<5进行选择,而不是针对特定课程:

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,))