基本上在应用程序中我连接到我的sql数据库并尝试检索用户给出的输入结果AGE
,SEX
和ADMITTING DIAGNOSIS CODE
。
我可以为AGE和SEX
提供用户输入并获得所需的结果,但是当我为AGE
,SEX
和ADMITTING_DIAGNOSIS_CODE
我的网页提供用户输入时返回NONE
个值。
Python代码:
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 = %s and SEX = %s and ADMITTING_DIAGNOSIS_CODE = %s and DISCHARGE_STATUS = "A")/(count(*))*100 as alive,(select count(*) from health where AGE = %s and SEX = %s and ADMITTING_DIAGNOSIS_CODE = %s and DISCHARGE_STATUS = "B")/(count(*))*100 as dead from health where AGE = %s and SEX = %s and ADMITTING_DIAGNOSIS_CODE = %s'
entries = []
cursor.execute(sql,(AGE,SEX,ADMITTING_DIAGNOSIS_CODE,AGE,SEX,ADMITTING_DIAGNOSIS_CODE,AGE,SEX,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()
HTML CODE:
<html>
<head>
<title> Welcome</title>
</head>
<body>
<h1> Hello World!!!!</h1>
<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>
</body>
</html>
<html>
<head>
<title> Welcome</title>
</head>
<body>
<form action="/value" method="get" enctype ="multipart/form-data">
<table style="border: 1px solid black">
<tbody>
<tr>
<th width="35%" style="background-color: #CCFFCC; margin: 5px">Length of stay</th>
<th style="background-color: #CCFFCC; margin: 5px">Total charge</th>
<th style="background-color: #CCFFCC; margin: 5px">Alive</th>
<th style="background-color: #CCFFCC; margin: 5px">Dead</th>
</tr>
{% for entry in entries %}
<tr>
<td>{{ entry['avg(LENGTH_OF_STAY)'] }}</td>
<td>{{ entry['avg(TOTAL_CHARGES)'] }}</td>
<td>{{ entry['alive'] }}</td>
<td>{{ entry['dead'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
</body>
</html>
输出:
Length of stay Total charge Alive Dead
None None None None