溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2022-03-24 16:24:20 來源:億速云 閱讀:214 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“python中SQLAlchemy怎么使用前端頁面實現插入數據”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“python中SQLAlchemy怎么使用前端頁面實現插入數據”文章能幫助大家解決問題。

1.實驗效果

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

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

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

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

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

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

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:
    # 創(chuàng)建數據庫: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)
#創(chuàng)建數據庫
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):
    #判斷數據庫表中是否已經存在了此用戶,如果存在,則不進行插入數據
    data=Moster.query.filter(Moster.username==username).all()
    if data==[]:
        # 創(chuàng)建對象,進行數據的插入
        mos = Moster(username=username, password=password)
        # 創(chuàng)建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()
    # 創(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>賬號: </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>

關于“python中SQLAlchemy怎么使用前端頁面實現插入數據”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI