我的代码允许我通过在数据库中搜索来显示与用户提供的数据相对应的结果。但我有这个错误,我没能解决这个错误。
MySQLdb._exceptions.ProgrammingError
MySQLdb._exceptions.ProgrammingError: execute() first
这是不同的部分#home.html
<p class="article-content">
<div class="form_form">
<form class="form" method="post" action="/search">
<label for="user_input"></label>
<input id="user_input" name="user_input" type="text">
<input type = "submit" value = "send">
</form>
</div>
</p>
#search_results.html
{% extends "layout.html" %}
{% block content %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<h5><a class="mr-2" href="#">results for{{ user_input }}</a></h5>
</div>
<p class="article-content"><p>Family Name: </p>{{ results.name }}</p>
<p class="article-content"><p>Family Description: </p>{{ results.description }}</p>
<p class="article-content"><p>Address: {{ results.address }}</p>
<p class="article-content"><p>Phone Number: {{ results.phone }}</p>
</div>
</article>
<form>
<input type="button" value="New Search" onclick="history.go(-1)">
</form>
{% endblock content %}
#路线.py
@app.route('/search', methods=['GET', 'POST'])
def search():
cur = db.connection.cursor()
if request.method == "POST":
user_input = request.form["user_input"]
cur.execute = ("SELECT f.family_name, f.family_description, f.family_address, f.family_phone FROM Shop f WHERE f.family_address LIKE %s ORDER BY family_name", ( "%" + user_input + "%",))
results = cur.fetchall()
return render_template('search_results.html', user_input=user_input, results=results)
else:
return redirect(url_for('home'))
此行:
cur.execute = ("SELECT f.family_name, f.family_description, f.family_address, f.family_phone FROM Shop f WHERE f.family_address LIKE %s ORDER BY family_name", ( "%" + user_input + "%",))
应为:
cur.execute("SELECT f.family_name, f.family_description, f.family_address, f.family_phone FROM Shop f WHERE f.family_address LIKE %s ORDER BY family_name", ( "%" + user_input + "%",))