简体   繁体   中英

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. Code I have so far is below.

I keep a werkzeug error:

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)

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.

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.

You can give the account_id variable in the method definition a default value and also check for its presence in the GET parameters:

@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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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