<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
MySQLdb和pymysql的使用差不多閱讀的小夥伴可以自己嘗試實現
import MySQLdb from flask_wtf import FlaskForm from wtforms.validators import DataRequired,EqualTo,Length from wtforms import StringField,SubmitField,PasswordField,TelField from flask import Flask,render_template,redirect,url_for,abort,request,jsonify app=Flask(__name__) app.secret_key='stu' #連線資料mysql conn=MySQLdb.connect( host='127.0.0.1', port=3306, user='root', password='root', db='main' ) cur=conn.cursor() #增加使用者表單 class StuForm(FlaskForm): name=StringField(label='使用者名稱: ',validators=[DataRequired()]) password=PasswordField(label='密碼: ',validators=[DataRequired(),Length(min=3,max=8)]) submit=SubmitField(label='提交') #搜尋使用者表單 class SStuForm(FlaskForm): name = StringField(label='使用者名稱: ', validators=[DataRequired()]) submit=SubmitField(label='提交') #更新使用者表單 class UStuForm(FlaskForm): name = StringField(label='使用者名稱: ', validators=[DataRequired()]) password = PasswordField(label='密碼: ', validators=[DataRequired(), Length(min=3, max=8)]) submit = SubmitField(label='提交') #刪除使用者表單 class DStuForm(FlaskForm): name = StringField(label='使用者名稱: ', validators=[DataRequired()]) submit = SubmitField(label='提交') def CreateTab(): sql="create table student(name varchar(20),password varchar(30))" cur.execute(sql) conn.commit() cur.close() @app.route('/add',methods=['POST','GET']) def add(): stuform=StuForm() if request.method=='POST': if stuform.validate_on_submit(): name=stuform.name.data password=stuform.password.data print('name: {}'.format(name)) print('password: {}'.format(password)) sql=f"insert into student(name,password) values('{name}','{password}')" cur.execute(sql) conn.commit() return jsonify('Add Successed!') return render_template('add.html',stuform=stuform) @app.route('/search',methods=['POST','GET']) def search(): sstuform=SStuForm() if request.method=='POST': if sstuform.validate_on_submit(): name=sstuform.name.data sql=f"select count(name) from student where name='{name}'" cur.execute(sql) conn.commit() count=cur.fetchone()[0] if count<=0: return jsonify('The User is not exist!') else: sql=f"select name,password from student where name='{name}'" cur.execute(sql) conn.commit() result=cur.fetchall() return jsonify(result) return render_template('search.html',sstuform=sstuform) @app.route('/update',methods=['POST','GET']) def update(): ustuform=UStuForm() if request.method=='POST': if ustuform.validate_on_submit(): name = ustuform.name.data password=ustuform.password.data sql = f"select count(name) from student where name='{name}'" cur.execute(sql) conn.commit() count = cur.fetchone()[0] if count <= 0: return jsonify('The User is not exist!') else: sql = f"update student set name='{name}',password='{password}' where name='{name}'" cur.execute(sql) conn.commit() return jsonify('Update Successed!') return render_template('update.html',ustuform=ustuform) @app.route('/delete',methods=['POST','GET']) def delete(): dstuform=DStuForm() if request.method=='POST': if dstuform.validate_on_submit(): name=dstuform.name.data sql = f"select count(name) from student where name='{name}'" cur.execute(sql) conn.commit() count = cur.fetchone()[0] if count <= 0: return jsonify('The User is not exist!') else: sql=f"delete from student where name='{name}'" cur.execute(sql) conn.commit() return jsonify('Delete Successed!') return render_template('delete.html',dstuform=dstuform) @app.route('/function',methods=['POST','GET']) def function(): if request.method=='POST': submit1 = request.form.get('submit1') submit2 = request.form.get('submit2') submit3 = request.form.get('submit3') submit4 = request.form.get('submit4') print('submit1: {}'.format(submit1)) print('submit2: {}'.format(submit2)) print('submit3: {}'.format(submit3)) print('submit4: {}'.format(submit4)) if submit1 is not None: return redirect(url_for('add')) if submit2 is not None: return redirect(url_for('search')) if submit3 is not None: return redirect(url_for('update')) if submit4 is not None: return redirect(url_for('delete')) return render_template('base.html') if __name__ == '__main__': print('Pycharm') # CreateTab() app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .h1{ position:relative; margin:auto; width:500px; height:50px; margin-top:100px; margin-left:650px; } .form { position:relative; width:500px; height:50px; margin:auto; margin-top:50px; border:2px solid #000000; color:#000000; font-size:20px; font-weight:400; } .form1{ position:absolute; margin-top:10px; margin-left:80px; } .form2{ position:absolute; margin-top:10px; margin-left:180px; } .form3{ position:absolute; margin-top:10px; margin-left:280px; } .form4{ position:absolute; margin-top:10px; margin-left:380px; } </style> </head> <body> <div class="h1"> <h1>功能選擇</h1> </div> <div class="form"> <form class="form1" action="http://127.0.0.1:5000/add" method="POST"> <input type="submit" name="submit1" value="新增"> </form> <form class="form2" action="http://127.0.0.1:5000/search" method="POST"> <input type="submit" name="submit2" value="搜尋"> </form> <form class="form3" action="http://127.0.0.1:5000/update" method="POST"> <input type="submit" name="submit3" value="更新"> </form> <form class="form4" action="http://127.0.0.1:5000/delete" method="POST"> <input type="submit" name="submit4" value="刪除"> </form> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Insert</title> <style> div{ width:255px; height:100px; margin:auto; margin-top:200px; border:2px solid #000000; font-size:20px; font-weight:400px; background:#FFFFFF; } .submit{ margin-top:10px; margin-left:100px; } </style> </head> <body> <div> <form action="" method="POST"> {{ustuform.csrf_token()}} {{ustuform.name.label}}{{ustuform.name}}<br> {{ustuform.password.label}}{{ustuform.password}}<br> <input class="submit" type="submit" name="submit" value="更新"> </form> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Insert</title> <style> div{ width:255px; height:100px; margin:auto; margin-top:200px; border:2px solid #000000; font-size:20px; font-weight:400px; background:#FFFFFF; } .submit{ margin-top:10px; margin-left:100px; } </style> </head> <body> <div> <form action="" method="POST"> {{dstuform.csrf_token()}} {{dstuform.name.label}}{{dstuform.name}}<br> <input class="submit" type="submit" name="submit" value="刪除"> </form> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Insert</title> <style> div{ width:255px; height:100px; margin:auto; margin-top:200px; border:2px solid #000000; font-size:20px; font-weight:400px; background:#FFFFFF; } .submit{ margin-top:10px; margin-left:100px; } </style> </head> <body> <div> <form action="" method="POST"> {{sstuform.csrf_token()}} {{sstuform.name.label}}{{sstuform.name}}<br> <input class="submit" type="submit" name="submit" value="搜尋"> </form> </div> </body> </html>
到此這篇關於MySQ Ldb 連線資料的使用的文章就介紹到這了,更多相關MySQLdb連線資料內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45