实现目标:前端输入一个ID,通过查询,返回数据库中这个ID的所有信息。
//由于主要目的是实现功能,所以界面巨丑
app.py
from flask import Flask,request,render_template
import db
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world'
@app.route('/test',methods=['POST','GET'])
def test():
if request.method=="GET":
print("GET")
return render_template('test.html',message="GET")
else:
print('POST')
id=request.form['ID']
message=db.get_data(id)
for i in message:
print(i)
return render_template('test.html',message=message,id=id)
if __name__ == '__main__':
app.run(debug=True)
db.py
from flask import Flask
import sqlite3
def get_data(id):
conn = sqlite3.connect('./db/course.db')
# 查询语句
id=int(id)
query_sql = '''
SELECT
*
FROM
table_name #修改
WHERE
CUST_CODE = %d
'''
##进行查询
tem=[]
query = conn.execute(query_sql % id)
for i in query:
tem.append(i)
conn.close()
return tem
然后通过运行app.py,就可以在网页中看到功能的呈现