[英]Run Cx_Oracle query in Flask App
我正在嘗試構建一個具有頁面的應用程序,在該頁面上我輸入ID並對該頁面運行查詢並顯示結果。 我到目前為止的代碼如下。
我有一個werkzeug錯誤:
BuildError: ('show_entries', {}, None)
app.py
import cx_Oracle
# Run the query to display the results
@app.route('/matcher/<int:account_id>', methods=['GET', 'POST'])
def show_entries(account_id):
sql = """SELECT item1,
item2,
item3,
item4,
item5,
item6
FROM TABLE
WHERE account_id = ?"""
c = g.db.cursor()
c.execute(sql, account_id)
您收到該錯誤的原因是show_entries
方法需要一個account_id
參數,但url_for
調用未提供該參數。
似乎您正在嘗試讓show_entries
方法將account_id
參數作為表單中的GET值使用,但作為方法定義中URL的一部分(而不是GET參數),因此不匹配。
您可以為方法定義中的account_id
變量賦予默認值,還可以檢查GET參數中是否存在該變量:
@app.route('/matcher/', methods=['GET', 'POST'])
@app.route('/matcher/<int:account_id>', methods=['GET', 'POST'])
def show_entries(account_id=0):
if request.method == 'GET' and not account_id:
account_id = request.args.get('account_id', 0)
...
文件: url_for , request.args.get 。
使這項工作起作用的附加功能在這里。 我的原始代碼中的所有其他內容都很好,即使不是最佳選擇。
c.execute(sql, account_id=account_id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.