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)