最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python在前端頁面使用?MySQLdb?連接數(shù)據(jù)

 更新時間:2022年03月24日 14:08:06   作者:Keep_Trying_Go  
這篇文章主要介紹了MySQLdb?連接數(shù)據(jù)的使用,文章主要介紹的相關(guān)內(nèi)容又插入數(shù)據(jù),刪除數(shù)據(jù),更新數(shù)據(jù),搜索數(shù)據(jù),需要的小伙伴可以參考一下

1.文件結(jié)構(gòu)

在這里插入圖片描述

MySQLdb和pymysql的使用差不多閱讀的小伙伴可以自己嘗試實現(xiàn)

2.實驗效果

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

3.主文件:main.py

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'

#連接數(shù)據(jù)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)

4.base.html文件

<!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>

5.update.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>

6.delete.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>

7.search.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>

到此這篇關(guān)于MySQ Ldb 連接數(shù)據(jù)的使用的文章就介紹到這了,更多相關(guān)MySQLdb連接數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python使用datetime模塊計算各種時間間隔的方法

    python使用datetime模塊計算各種時間間隔的方法

    這篇文章主要介紹了python使用datetime模塊計算各種時間間隔的方法,實例分析了Python使用datetime模塊進(jìn)行各種常用的時間操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 對python中兩種列表元素去重函數(shù)性能的比較方法

    對python中兩種列表元素去重函數(shù)性能的比較方法

    今天小編就為大家分享一篇對python中兩種列表元素去重函數(shù)性能的比較方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • PyTorch、torchvision和Python版本的對應(yīng)關(guān)系

    PyTorch、torchvision和Python版本的對應(yīng)關(guān)系

    使用PyTorch時,選擇合適的Python版本是至關(guān)重要的,錯誤的版本組合可能導(dǎo)致各種兼容性問題,本文就來介紹一下PyTorch、torchvision與Python版本匹配,感興趣的可以了解一下
    2024-03-03
  • CentOS7上使用pyenv搭建Django環(huán)境

    CentOS7上使用pyenv搭建Django環(huán)境

    本文主要介紹了CentOS7上使用pyenv搭建Django環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • python 元組的使用方法

    python 元組的使用方法

    這篇文章主要介紹了python 元組的使用方法,文中講解非常細(xì)致,代碼幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • python爬蟲分布式獲取數(shù)據(jù)的實例方法

    python爬蟲分布式獲取數(shù)據(jù)的實例方法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python爬蟲分布式獲取數(shù)據(jù)的實例方法,有興趣的朋友們可以參考下。
    2020-11-11
  • Python提高運行速度工具之Pandarallel的使用教程

    Python提高運行速度工具之Pandarallel的使用教程

    為了提高運行速度,我們一般會采用多進(jìn)程的方式。而常見的方案對于普通python玩家來說都不是特別友好,怎樣才能算作一個友好的并行處理方案?本文就來和大家講講pandarallel的使用
    2022-09-09
  • Python如何利用xlrd和xlwt模塊操作Excel表格

    Python如何利用xlrd和xlwt模塊操作Excel表格

    這篇文章主要給大家介紹了關(guān)于Python如何利用xlrd和xlwt模塊操作Excel表格的相關(guān)資料,其中xlrd模塊實現(xiàn)對excel文件內(nèi)容讀取,xlwt模塊實現(xiàn)對excel文件的寫入,需要的朋友可以參考下
    2022-03-03
  • python中日期和時間格式化輸出的方法小結(jié)

    python中日期和時間格式化輸出的方法小結(jié)

    這篇文章主要介紹了python中日期和時間格式化輸出的方法,實例總結(jié)了Python常見的日期與事件操作技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • python 調(diào)整圖片亮度的示例

    python 調(diào)整圖片亮度的示例

    這篇文章主要介紹了python 調(diào)整圖片亮度的示例代碼,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
    2020-12-12

最新評論

武威市| 明溪县| 陵水| 平阴县| 错那县| 西乌珠穆沁旗| 巩义市| 芦溪县| 桂林市| 扬中市| 湾仔区| 鄂州市| 临海市| 宝鸡市| 巩义市| 互助| 泾源县| 江门市| 隆回县| 澜沧| 张家界市| 四子王旗| 和硕县| 静安区| 会理县| 沽源县| 东海县| 桓台县| 韶关市| 威远县| 独山县| 洪雅县| 江口县| 丁青县| 江孜县| 庆阳市| 荥阳市| 浦江县| 张家港市| 丽水市| 徐州市|