pythonweb项目描述_Python-web项目基于基于flask框架

fromflask import Flask, request, render_template, redirect, url_for

import pymysql

import json

app=Flask(__name__)

@app.route('/')

def index():return render_template('allBook.html')

@app.route('/getAll',methods=['POST','GET'])

def getAll():try:

contect=getConnect()

cur=contect.cursor()

cur.execute("select * from books")

jsonData=[]for row incur.fetchall():

result={}

result['bookID'] = row[0]

result['bookName'] = row[1]

result['bookCounts'] = row[2]

result['detail'] = row[3]

jsonData.append(result)returnjson.dumps(jsonData)

except Exceptionase:

print("失败", e)finally:

cur.close()

contect.close()

@app.route('/deleteBook',methods=['POST','GET'])

def deleteBook():

BookID= request.form['bookID']try:

contect=getConnect()

mycursor=contect.cursor()

mycursor.execute("delete from books where bookID ="+BookID)

contect.commit()return "yes"except Exceptionase:

print("失败", e)finally:

mycursor.close()

contect.close()

@app.route('/addBook',methods=['POST','GET'])

def addBook():

bookName= request.form['bookName']

bookCounts= request.form['bookCounts']

detail= request.form['detail']try:

contect=getConnect()

mycursor=contect.cursor()

sql= "insert into books (bookName, bookCounts, detail) values ("+str(bookName)+","+bookCounts+","+str(detail)+")"print(sql)

mycursor.execute(sql)

contect.commit()return render_template("allBook.html")

except Exceptionase:

print("失败", e)finally:

mycursor.close()

contect.close()

@app.route("/toaddBook")

def toaddBook():return render_template('addBook.html')

@app.route("/toUpdateBook")

def toUpdateBook():try:

bookID= request.url.split("?")[1]

contect=getConnect()

mycursor=contect.cursor()

sql= "select * from books where bookID =" +bookID

mycursor.execute(sql)

jsonData=[]

row=mycursor.fetchone()

result={}

result['bookID'] = row[0]

result['bookName'] = row[1]

result['bookCounts'] = row[2]

result['detail'] = row[3]return render_template('updateBook.html',jsonData =result)

except Exceptionase:

print("失败", e)finally:

mycursor.close()

contect.close()

@app.route('/updateBook',methods=['POST','GET'])

def updateBook():

bookID= request.form['bookID']

bookName= request.form['bookName']

bookCounts= request.form['bookCounts']

detail= request.form['detail']try:

contect=getConnect()

mycursor=contect.cursor()

sql= "update books set bookName='"+str(bookName)+"',bookCounts='"+bookCounts+"',detail='"+str(detail)+"' where bookID="+bookID

print(sql)

mycursor.execute(sql)

contect.commit()return render_template("allBook.html")

except Exceptionase:

print("失败", e)finally:

mycursor.close()

contect.close()

@app.route("/queryBook",methods=['POST','GET'])

def queryBook():try:

bookName= request.form['queryBookName']

contect=getConnect()

mycursor=contect.cursor()

sql= "select * from books where bookName like" + "'%"+bookName+"%'"print(sql)

mycursor.execute(sql)

jsonData=[]

row=mycursor.fetchone()

result={}

result['bookID'] = row[0]

result['bookName'] = row[1]

result['bookCounts'] = row[2]

result['detail'] = row[3]return render_template("queryBook.html",result =result)

except Exceptionase:

print("失败", e)finally:

mycursor.close()

contect.close()

def getConnect():return pymysql.Connect(host='localhost',port=3306,user="root",passwd="511924..",db="ssmbuild",charset="utf8")if __name__ == '__main__':

app.run(debug=True,port=8033)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值