简体   繁体   中英

My python flask application returns 400 Bad request

Basically in this application i am connecting to my sql database and trying to retrieve results for the inputs given by user which are AGE,SEX and ADMITTING DIAGNOSIS CODE.I am able to give user input for both AGE and SEX and get the required results but when i give user input for ADMITTING_DIAGNOSIS_CODE it returns 400 BAD REQUEST.

My Python code:

import MySQLdb
from flask import Flask, render_template, request
from flask.ext.mysqldb import MySQL

app = Flask(__name__)

db = MySQLdb.connect("127.0.0.1","root","","health" )

@app.route("/", methods = ['GET','POST'])
def home():
    return render_template('home.html')




@app.route("/value", methods = ['GET','POST'])
def Authenticate():
    cursor = db.cursor()
    AGE = request.form['AGE']
    SEX = request.form['SEX']
    ADMITTING_DIAGNOSIS_CODE = request.form['ADMITTING_DIAGNOSIS_CODE']
    #DIAGNOSIS_CODE_1= request.args['DIAGNOSIS_CODE_1']

    sql = 'select avg(LENGTH_OF_STAY),avg(TOTAL_CHARGES),(select count(*) from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = %s and DISCHARGE_STATUS = "A")/(count(*))*100 as alive,(select count(*) from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = 5849 and DISCHARGE_STATUS = "B")/(count(*))*100 as dead from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = 5849'

    entries = []
    cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE))

    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    for row in results:
        entries.append(dict([('avg(LENGTH_OF_STAY)',row[0]),
                             ('avg(TOTAL_CHARGES)',row[1]),
                             ('dead',row[3]),
                             ('alive',row[2])

                             ]))

    return render_template('show_entries.html', entries=entries)

if __name__ == "__main__":
    app.debug = True
    app.run()

My HTML code:

**

> <form action="/value" method="post" enctype ="multipart/form-data">
> <div>Enter the Age <input type="text" name="AGE" style="border: 1px
> solid black"></div> <div>Enter the Sex <input type="text" name="SEX"
> style="border: 1px solid black"></div> <div>Enter the code <input
> type="text" name="ADMITTING_DIAGNOSIS_CODE" style="border: 1px solid
> black"></div> <div><input type="submit" value=" GO"></div> </form>

**

Please help me out.

This line

cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE))

should be

cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE,))

The difference being that (x) is equal to x while (x,) is equal to the single value tuple containing the first argument x.

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