繁体   English   中英

在Flask App中运行Cx_Oracle查询

[英]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_forrequest.args.get

使这项工作起作用的附加功能在这里。 我的原始代码中的所有其他内容都很好,即使不是最佳选择。

        c.execute(sql, account_id=account_id)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM