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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python中SQLAlchemy使用前端頁(yè)面實(shí)現(xiàn)插入數(shù)據(jù)_python

作者:Keep_Trying_Go ? 更新時(shí)間: 2022-05-27 編程語(yǔ)言

1.實(shí)驗(yàn)效果

在這里插入圖片描述

在這里插入圖片描述

如果插入的數(shù)據(jù)已經(jīng)存在于數(shù)據(jù)庫(kù)中,則出現(xiàn)以下提示:

查看數(shù)據(jù)庫(kù)表中的數(shù)據(jù),發(fā)現(xiàn)已經(jīng)將數(shù)據(jù)存入了數(shù)據(jù)庫(kù)表中:

在這里插入圖片描述

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:
? ? """相關(guān)配置"""
? ? # cmd:
? ? # 創(chuàng)建數(shù)據(jù)庫(kù):create database flaskdb(數(shù)據(jù)庫(kù)名) default charset(類型) utf8;
? ? # 使用數(shù)據(jù):use flaskdb
? ? # 查看數(shù)據(jù)庫(kù)表:show tables;
? ? SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:root@127.0.0.1:3306/flaskdb'
? ? SQLALCHEMY_TRACK_MODIFICATIONS=True

app.config.from_object(Config)
#創(chuàng)建數(shù)據(jù)庫(kù)
mysql=SQLAlchemy(app)
#創(chuàng)建表
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):
? ? #判斷數(shù)據(jù)庫(kù)表中是否已經(jīng)存在了此用戶,如果存在,則不進(jìn)行插入數(shù)據(jù)
? ? data=Moster.query.filter(Moster.username==username).all()
? ? if data==[]:
? ? ? ? # 創(chuàng)建對(duì)象,進(jìn)行數(shù)據(jù)的插入
? ? ? ? mos = Moster(username=username, password=password)
? ? ? ? # 創(chuàng)建session
? ? ? ? mysql.session.add(mos)
? ? ? ? mysql.session.commit()
? ? ? ? # 關(guān)閉數(shù)據(jù)庫(kù)
? ? ? ? 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')
? ? # 對(duì)數(shù)據(jù)庫(kù)進(jìn)行清除,讓數(shù)據(jù)庫(kù)是“干凈的”
? ? # mysql.drop_all()
? ? # 創(chuàng)建表
? ? 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>賬號(hào): </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

欄目分類
最近更新