日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中SQLAlchemy使用前端頁面實現插入數據_python

作者:Keep_Trying_Go ? 更新時間: 2022-05-27 編程語言

1.實驗效果

在這里插入圖片描述

在這里插入圖片描述

如果插入的數據已經存在于數據庫中,則出現以下提示:

查看數據庫表中的數據,發現已經將數據存入了數據庫表中:

在這里插入圖片描述

2.主main.py文件

import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import String,Integer,create_engine,Column
from flask import Flask,render_template,redirect,request,url_for,abort,jsonify

app=Flask(__name__)

class Config:
? ? """相關配置"""
? ? # cmd:
? ? # 創建數據庫:create database flaskdb(數據庫名) default charset(類型) utf8;
? ? # 使用數據:use flaskdb
? ? # 查看數據庫表:show tables;
? ? SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:root@127.0.0.1:3306/flaskdb'
? ? SQLALCHEMY_TRACK_MODIFICATIONS=True

app.config.from_object(Config)
#創建數據庫
mysql=SQLAlchemy(app)
#創建表
class Moster(mysql.Model):
? ? """管理員表名"""
? ? __tablename__='moster'
? ? username=Column(String(128),primary_key=True)
? ? password=Column(String(128),unique=True)

@app.route('/<string:username>/<string:password>',methods=['POST','GET'])
def Insert_User(username,password):
? ? #判斷數據庫表中是否已經存在了此用戶,如果存在,則不進行插入數據
? ? data=Moster.query.filter(Moster.username==username).all()
? ? if data==[]:
? ? ? ? # 創建對象,進行數據的插入
? ? ? ? mos = Moster(username=username, password=password)
? ? ? ? # 創建session
? ? ? ? mysql.session.add(mos)
? ? ? ? mysql.session.commit()
? ? ? ? # 關閉數據庫
? ? ? ? mysql.session.close()
? ? ? ? return jsonify('Add the data Successed!')
? ? else:
? ? ? ? return jsonify('The data have been existed!')


@app.route('/index',methods=['POST','GET'])
def index():
? ? if request.method=='POST':
? ? ? ? username=request.form.get('username')
? ? ? ? password=request.form.get('password')
? ? ? ? return redirect(url_for('Insert_User',username=username,password=password))
? ? return render_template('mysql.html')

if __name__ == '__main__':
? ? print('Pycharm')
? ? # 對數據庫進行清除,讓數據庫是“干凈的”
? ? # mysql.drop_all()
? ? # 創建表
? ? mysql.create_all()
? ? app.run(debug=True)

3.前端mysql.html文件

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>MySQL</title>
? ? <style>
? ? ? ? div {
? ? ? ? ? ? width:250px;
? ? ? ? ? ? height:100px;
? ? ? ? ? ? margin:auto;
? ? ? ? ? ? margin-top:200px;
? ? ? ? ? ? font-size:15px;
? ? ? ? ? ? font-weight:700;
? ? ? ? ? ? border:2px solid #000000;
? ? ? ? ? ? background:#FFFFFF;
? ? ? ? }
? ? ? ? div form input {
? ? ? ? ? ? margin-top:10px;
? ? ? ? }
? ? ? ? .btn{
? ? ? ? ? ? margin-left:100px;
? ? ? ? ? ? cursor:pointer;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div>
? ? ? ? <form action="http://127.0.0.1:5000/index" method="POST">
? ? ? ? ? ? <label>賬號: </label>
? ? ? ? ? ? <input type="text" name="username"><br>
? ? ? ? ? ? <label>密碼: </label>
? ? ? ? ? ? <input type="password" name="password"><br>
? ? ? ? ? ? <input class="btn" type="submit" name="submit" value="提交"><br>
? ? ? ? </form>
? ? </div>
</body>
</html>

原文鏈接:https://mydreamambitious.blog.csdn.net/article/details/123537523

欄目分類
最近更新