python实战之flask操作mysql数据库实战代码

from flask import Flask, request, jsonify
import pymysql
from flask_cors import CORS

db = pymysql.connect("127.0.0.1", "root", "root", "ccflow")
cursor = db.cursor()  # 数据库指针

app = Flask(__name__)
CORS(app, resources=r'/*')  # 解决跨域问题


@app.route("/list", methods=['POST', 'GET'])
def list():
    if request.method == "POST":  # 判断是否为post模式
        cursor.execute("select name,pass,email,tel from port_emp")
        data = cursor.fetchall();
        temp = {}
        result = []
        if (data != None):
            for i in data:
                temp["name"] = i[0]
                temp["pass"] = i[1]
                temp["email"] = i[2]
                temp["tel"] = i[3]
                result.append(temp.copy())
            print(len(data))
            return jsonify(result)
        else:
            print("result:NULL取到空")
            return jsonify([])
        # username = request.form.get("username")
        # print("!!!!!!!!" + str(username))
    else:
        return "get模式"


@app.route("/add", methods=['POST'])
def add():
    if request.method == "POST":  # 判断是否为post模式
        no = request.form.get("no")
        name = request.form.get("name")
        vpass = request.form.get("pass")
        email = request.form.get("email")
        tel = request.form.get("tel")
        try:
            cursor.execute(
                "insert into port_emp(no,name,pass,email,tel) values (\"" + str(no)
                + "\",\"" + str(name) + "\",\"" + str(vpass) + "\",\""
                + str(email) + "\",\"" + str(tel) + "\")")
            db.commit()
            print("ok!!!!!!!!!!!!!!!!")
            return "1"
        except Exception as e:
            print("add---failed", e)
            db.rollback()
            return "-1"


@app.route("/del", methods=['POST'])
def ddel():
    if request.method == "POST":  # 判断是否为post模式
        no = request.form.get("no")
        try:
            cursor.execute("Delete from port_emp where no =" + str(no))
            db.commit()
            print("del---ok!!!!!!!!!!!!!!!!")
            return "1"
        except Exception as e:
            print("del---failed", e)
            db.rollback()
            return "-1"


@app.route("/update", methods=['POST'])
def update():
    if request.method == "POST":  # 判断是否为post模式
        no = request.form.get("no")
        tel = request.form.get("tel")
        try:
            cursor.execute("update port_emp set tel=\"" + str(tel) + "\" where no=\"" + str(no) + "\"")
            db.commit()
            print("update---ok!!!!!!!!!!!!!!!!")
            return "1"
        except Exception as e:
            print("update---failed", e)
            db.rollback()
            return "-1"


@app.route("/vlogin", methods=['POST'])
def vlogin():
    if request.method == "POST":  # 判断是否为post模式
        username = request.form.get("username")
        password = request.form.get("password")
        cursor.execute(
            "select no,name,tel,pass from port_emp where name=\"" + str(username) + "\" and pass=\"" + str(
                password) + "\"")
        data = cursor.fetchone()
        if (data != None):
            print("result:", data)
            jsondata = {"name": str(data[0]), "tel": str(data[2]), "pass": str(data[3])}
            return jsonify(jsondata)
        else:
            print("result:NULL")
            jsondata = {}
            return jsonify(jsondata)


@app.route("/count", methods=['POST'])
def count():
    if request.method == "POST":  # 判断是否为post模式
        name = request.form.get("name")
        try:
            cursor.execute("update port_emp set idx=idx+1 where name=\""+str(name)+"\"")
            db.commit()
            print("count---ok!!!!!!!!!!!!!!!!")
            return "1"
        except Exception as e:
            print("count---failed", e)
            db.rollback()
            return "-1"

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)
    db.close()
    print("bye!!!!!!!!!!!!!")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值