博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网页端数据库操作界面—主题函数文件
阅读量:2094 次
发布时间:2019-04-29

本文共 5169 字,大约阅读时间需要 17 分钟。

这里是函数调用代码

from flask import Flask, render_template, requestimport DBUtilsOapp = Flask(__name__)@app.route("/")def hello():    return render_template("hello.html")@app.route("/login")def login():    return render_template("login.html")@app.route("/F5",methods=["get","post"])def F5():    sql = "select id,name,EMAIL,TELS from TB_DATA"    tuple = DBUtilsO.selectData(sql)    list = cardTubleToDic(tuple)    return render_template("list.html",list = list)@app.route("/toSearch",methods=["get","post"])def toSearch():    name = request.form.get("name")    idm = request.form.get("idm")    print(name)    sql = "select ID,NAME,EMAIL,TELS from TB_DATA"    if len(name)>0:        sql = "select ID,NAME,EMAIL,TELS from TB_DATA where NAME like '%%%s%%' "%name    if len(idm)>0:        sql = "select ID,NAME,EMAIL,TELS from TB_DATA where ID = %s "%idm    tuple = DBUtilsO.selectData(sql)    list = cardTubleToDic(tuple)    return render_template("list.html",list = list)def cardTubleToDic(tuple):    list = []    for obj in tuple:        list.append({
"id":obj[0], "name":obj[1], "email":obj[2], "tels":obj[3] }) return list@app.route("/toDelete",methods=["get","post"])def toDelete(): idm = request.args.get("id") sql = "delete from TB_DATA where id="+idm DBUtilsO.deleteData(sql) sql = "select id, name, email, tels from TB_DATA" tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) return render_template("list.html", list=list)@app.route("/insert",methods=["get","post"])def insert(): sql = "select id, name, email, tels from TB_DATA" tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) return render_template("insert.html",list=list)@app.route("/insertSucceed",methods=["get","post"])def insertSucceed(): idm = request.form.get("idm") name = request.form.get("name") email = request.form.get("email") tels = request.form.get("tels") sql = "select id, name, email, tels from TB_DATA" tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) lisName = [] lisId = [] for obj in list: lisName.append(obj['name']) lisId.append(obj['id']) if len(name)==0: return render_template("insert.html", msg="名称不能为空",list=list) if len(idm)==0: return render_template("insert.html", msg="ID不能为空",list=list) if name in lisName: return render_template("insert.html", msg="名称已存在",list=list) if idm in lisId: return render_template("insert.html", msg="ID已存在",list=list) sql = "INSERT INTO TB_DATA(ID,NAME,EMAIL,TELS) VALUES ('%s','%s','%s','%s')" % (idm, name, email, tels) a = DBUtilsO.insertData(sql) sql = "select ID,NAME,EMAIL,TELS from TB_DATA" tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) if a: return render_template("list.html", list=list, msg='添加成功') else: return render_template("list.html", list=list, msg='添加失败')@app.route("/Update",methods=["get","post"])def Update(): idm = request.args.get("id") sql = "select ID,NAME,EMAIL,TELS from TB_DATA where ID = %s " % idm tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) return render_template("Update.html",id=idm,list=list)@app.route("/UpdateSucceed",methods=["get","post"])def UpdateSucceed(): idm = request.args.get("id") name = request.form.get("name") emil = request.form.get("email") tel = request.form.get("tels") sql = "select NAME from TB_DATA" tuple = DBUtilsO.selectData(sql) lism = [] for obj in tuple: lism.append(obj[0]) if name in lism: return render_template("Update.html", msg="名称已存在") sql = "update TB_DATA set NAME ='%s',EMAIL='%s',TELS='%s' where ID=%s"%(name,emil,tel,idm) print(sql) DBUtilsO.updateData(sql) sql = "select ID,NAME,EMAIL,TELS from TB_DATA" tuple = DBUtilsO.selectData(sql) list = cardTubleToDic(tuple) return render_template("list.html", list=list)@app.route("/gotologin",methods=["get","post"])def gotologin(): name = request.form.get("name") pwd = request.form.get("pwd") # 根据name拿数据 sql = "select NAME,PWD from TB_USER where NAME='%s'"%name lism = DBUtilsO.selectData(sql) if len(lism) == 0: return render_template("login.html",msg="用户名不存在") elif lism[0][1] != pwd: return render_template("login.html",msg="密码不对") else: return render_template("list.html")if __name__ == "__main__": app.run(debug=True,port=5300)

其中,调用的数据库(oracle)操作代码为:

import cx_Oracledef getConnect():    conn = cx_Oracle.connect('SCOTT/adiga')    return conndef closeConnect(cursor, conn):    if cursor:        cursor.close()    if conn:        conn.close()def insertData(sql):    conn = getConnect()    cursor = conn.cursor()    cursor.execute(sql)    conn.commit()    count = cursor.rowcount    closeConnect(cursor, conn)    if count > 0:        return True    else:        return Falsedef selectData(sql):    conn = getConnect()    cursor = conn.cursor()    cursor.execute(sql)    a = cursor.fetchall()    closeConnect(cursor, conn)    return adef updateData(sql):    return insertData(sql)def deleteData(sql):    return insertData(sql)

转载地址:http://ubuhf.baihongyu.com/

你可能感兴趣的文章
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>