本页面写了一个登陆页面和一个查询页面:查询页面有增删改查和导出数据到excel文件的功能
登陆页面:loging_myself.html
查询页面:query_myself.html
后台py文件:query_myself.py
使用flask框架首先要创建一个项目,然后再项目中创建虚拟环境,如下,one为我创建的项目,fir是我创建的虚拟环境,写的html文件要放在templates中,这是硬性规定
此外,还需要引入
目录
loging_myself.html
query_myself.html
query_myself.py
Demo
layui
# 引入Flask
from flask import Flask, render_template, request,jsonify,json
import cx_Oracle
import logging
# 创建Flask实例
one = Flask(__name__)
#填入你要连接的数据库资料
user = ''
password = ''
host = ''
service_name = ''
conn_str = f"{user}/{password}@{host}/{service_name}"
#定义一个函数,用于在前端没有任何输入值的情况下默认查询出前数100条数据
def data(id):
connect = cx_Oracle.connect(conn_str)
cursor = connect.cursor()
datalist = []
if id:
sql=f"SELECT ID,QUERY,TEXT FROM MYQUERY where ID='{id}'"
cursor.execute(sql)
res=cursor.fetchall()
else:
sql="SELECT ID,QUERY,TEXT FROM MYQUERY where rownum<=100"
cursor.execute(sql)
res=cursor.fetchall()
cursor.close()
connect.close()
for i in res:
item_date = {
"ID": i[0],
"QUERY": i[1],
"TEXT": i[2]
}
datalist.append(item_date)
return datalist
#主頁面路由
@one.route('/')
def login_test():
one.logger.info('info log')
one.logger.warning('warning log')
return render_template('login_myself.html')
#登录路由
@one.route('/login_myself', methods=['GET','POST'])
def login():
data = request.form.get("datas")
print("data1:",data)#data1: {"username":"Q22000109","password":"123456"}
print(type(data))#
post_data = json.loads(data)#将post_data从JSON字符串转换为Python对象,json對象是一個鍵值形式的字符串,要轉換成
#python對象python才能識別
print("transferdata:",post_data)#transferdata: {'username': 'Q22000109', 'password': '123456'}
print("transfer",type(post_data))#transfer
connect = cx_Oracle.connect(conn_str)
cursor = connect.cursor()
sql="select count(*) from OP WHERE OP='%s' and pass='%s'"%(post_data['username'],post_data['password'])
print("sql is :",sql)#sql is : select count(*) from OP WHERE OP='Q22000109' and pass='123456'
cursor.execute(sql)
res=cursor.fetchall()
print(res)#[(1,)]
print(type(res))#
cursor.close()
connect.close()
a=0
if res[0][0] == a:
return_res = {
"code": 0,
"msg": "您没有权限",
"count": 1,
"data": data
}
else:
return_res = {
"code": 0,
"msg": "登陆成功",
"count": 1,
"data": data
}
return jsonify(return_res)# 然后使用“jsonify”将“return_res”字典作为 JSON 响应返回
@one.route('/query_myself', methods=['GET','POST'])
def query():
return render_template('query_myself.html')
#定义一个路由,在前端没有传进任何数据的情况下调用data函数
@one.route('/searchall', methods=['GET','POST'])
def searchall():
id=0
datalist = data(id)
count = len(datalist)
return_res = {
"code": 0,
"msg": "success",
"count": count,
"data": datalist
}
return jsonify(return_res)
@one.route('/search', methods=['GET','POST'])
def search():
post_data = request.form.get("searchParams")#是从前端发送到后端的POST请求中的一个参数名,代表搜索参数
# 通过这个参数名,后端可以获取前端用户输入的搜索关键词等信息,从而进行相应的搜索操作。
post_data = json.loads(post_data)#将post_data从JSON字符串转换为Python对象,json對象是一個鍵值形式的字符串,要轉換成
#python對象python才能識別
datalist = data(post_data['ID'])# 'ID' 键是从 'post_data' 对象中提取的,用于从 'data' 函数中检索数据
print("DATALIST",datalist)#这里返回的是一个列表,列表里面是很多个字典
count = len(datalist)
return_res = {
"code": 0,
"msg": "請求成功",
"count": count,
"data": datalist
}
return jsonify(return_res)
@one.route('/add', methods=['GET','POST'])
def adddata():
datas=request.form.get("datas")
print("getdatas:",datas)#getdatas: {"ID":"11","QUERY":"1","TEXT":"1"}
print("datas:",type(datas))#datas:
post_data = json.loads(datas)
print("post_data:",type(post_data))
connect = cx_Oracle.connect(conn_str)
cursor = connect.cursor()
sql="INSERT INTO MYQUERY VALUES('%s','%s','%s')"%(post_data['ID'],post_data['QUERY'],post_data['TEXT'])
print("sql is :",sql)
cursor.execute(sql)
connect.commit()
cursor.close()
connect.close()
return_res = {
"code": 0,
"msg": "添加成功",
"count": 1,
"data": post_data
}
return jsonify(return_res)
@one.route('/delete', methods=['GET','POST'])
def delete():
data = request.form.get("datas")
print("data1:",data)#data1: [{"ID":11,"QUERY":"1","TEXT":"1"}]
print(type(data))
post_data = json.loads(data)
print("transferdata:",post_data)
print("transfer",type(post_data))
connect = cx_Oracle.connect(conn_str)
cursor = connect.cursor()
nrows = len(post_data)
for i in range(nrows):
sql="delete from myquery where id = '%d'"%(post_data[0]['ID'])
print("sql is :",sql)
cursor.execute(sql)
connect.commit()
cursor.close()
connect.close()
return_res = {
"code": 0,
"msg": "删除成功",
"count": i,
"data": data
}
return jsonify(return_res)# 然后使用“jsonify”将“return_res”字典作为 JSON 响应返回
@one.route('/edit', methods=['GET','POST'])
def edit():
data = request.form.get("datas")
print("data1:",data)#data1: [{"ID":11,"QUERY":"1","TEXT":"1"}]
print(type(data))
# post_data = json.loads(data)
post_data = eval(data)
print("transferdata:",post_data)
print("transfer",type(post_data))
connect = cx_Oracle.connect(conn_str)
cursor = connect.cursor()
nrows = len(post_data)
for i in range(nrows):
sql="UPDATE MYQUERY SET QUERY='%s' ,TEXT='%s' WHERE ID='%s'"%(post_data[i]['QUERY'],post_data[i]['TEXT'],post_data[i]['ID'])
print("sql is :",sql)
cursor.execute(sql)
connect.commit()
cursor.close()
connect.close()
return_res = {
"code": 0,
"msg": "更新成功",
"count": i,
"data": data
}
return jsonify(return_res)# 然后使用“jsonify”将“return_res”字典作为 JSON 响应返回
if __name__ == '__main__':
handler = logging.FileHandler('flask.log')
one.logger.addHandler(handler)
one.run(debug=True,port=15002)