简体   繁体   English

在Flask App中运行Cx_Oracle查询

[英]Run Cx_Oracle query in Flask App

I am trying to build an app that has a page where I input an id and run a query on that and show results. 我正在尝试构建一个具有页面的应用程序,在该页面上我输入ID并对该页面运行查询并显示结果。 Code I have so far is below. 我到目前为止的代码如下。

I keep a werkzeug error: 我有一个werkzeug错误:

BuildError: ('show_entries', {}, None)

app.py 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)

You are receiving that error because your show_entries method is expecting an account_id argument, but your url_for call isn't supplying one. 您收到该错误的原因是show_entries方法需要一个account_id参数,但url_for调用未提供该参数。

It looks like you're trying to have the show_entries method take the account_id argument as a GET value in your form, but as part of the URL (not a GET parameter) in the method definition, so you have a mismatch. 似乎您正在尝试让show_entries方法将account_id参数作为表单中的GET值使用,但作为方法定义中URL的一部分(而不是GET参数),因此不匹配。

You can give the account_id variable in the method definition a default value and also check for its presence in the GET parameters: 您可以为方法定义中的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)
    ...

Docs: url_for , request.args.get . 文件: url_forrequest.args.get

The addition that makes this work is here. 使这项工作起作用的附加功能在这里。 Everything else in my original code is fine, even if not optimal. 我的原始代码中的所有其他内容都很好,即使不是最佳选择。

        c.execute(sql, account_id=account_id)

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

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