from flask import Flask,render_template,request,redirect from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@127.0.0.1:3306/epidemic2' class Donor(db.Model): donor_code = db.Column(db.String(255),primary_key=True) donor_name = db.Column(db.String(255)) type1 = db.Column(db.String(255)) ded_name = db.Column(db.String(255)) ded_code = db.Column(db.String(255)) amount = db.Column(db.Integer) date = db.Column(db.String(255)) trans = db.Column(db.String(255)) db.create_all() @app.route("/select") def selectAll(): donorList = Donor.query.all() return render_template("index.html", donor_list=donorList) @app.route('/') def index(): return selectAll() # 添加数据 @app.route('/insert', methods=['GET', 'POST']) def insert(): # 进行添加操作 donor_code = request.form['donor_code'] donor_name = request.form['donor_name'] type1 = request.form['type1'] ded_name = request.form['ded_name'] ded_code = request.form['ded_code'] amount = request.form['amount'] date = request.form['date'] trans = request.form['trans'] donor = Donor(donor_code=donor_code, donor_name=donor_name, type1=type1, ded_name=ded_name, ded_code=ded_code, amount= amount, date=date, trans=trans) db.session.add(donor) db.session.commit() # 添加完成重定向至主页 return redirect('/') @app.route("/insert_page") def insert_page(): # 跳转至添加信息页面 return render_template("insert.html") # 删除数据 @app.route("/delete", methods=['GET']) def delete(): # 操作数据库得到目标数据,before_number表示删除之前的数量,after_name表示删除之后的数量 donor_code = request.args.get("donor_code") donor = Donor.query.filter_by(donor_code=donor_code).first() db.session.delete(donor) db.session.commit() return redirect('/') # 修改操作 @app.route("/alter", methods=['GET', 'POST']) def alter(): # 可以通过请求方式来改变处理该请求的具体操作 # 比如用户访问/alter页面 如果通过GET请求则返回修改页面 如果通过POST请求则使用修改操作 if request.method == 'GET': donor_code = request.args.get("donor_code") donor_name = request.args.get("donor_name") type1 = request.args.get("type1") ded_name = request.args.get("ded_name") ded_code = request.args.get("ded_code") amount = request.args.get("amount") date = request.args.get("date") trans = request.args.get("trans") donor = Donor(donor_code=donor_code, donor_name=donor_name, type1=type1, ded_name=ded_name,ded_code=ded_code, amount=amount, date=date, trans=trans) return render_template("alter.html", donor=donor) else: donor_code = request.form["donor_code"] donor_name = request.form['donor_name'] type1 = request.form['type1'] ded_name = request.form['ded_name'] ded_code = request.form['ded_code'] amount = request.form['amount '] date = request.form['date'] trans = request.form['trans'] donor = Donor.query.filter_by(donor_code=donor_code).first() donor.donor_name = donor_name donor.type1 = type1 donor.ded_name = ded_name donor.ded_code = ded_code donor.amount = amount donor.date = date donor.trans = trans db.session.commit() return redirect('/') if __name__ == "__main__": app.run(debug=True)