python中SQLAlchemy使用前端頁(yè)面實(shí)現(xiàn)插入數(shù)據(jù)
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(類(lèi)型) 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)存在了此用戶(hù),如果存在,則不進(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>到此這篇關(guān)于SQLAlchemy使用前端頁(yè)面實(shí)現(xiàn)插入數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SQLAlchemy插入數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 給定的經(jīng)緯度標(biāo)注在地圖上的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 給定的經(jīng)緯度標(biāo)注在地圖上的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
python 實(shí)現(xiàn)圖片旋轉(zhuǎn) 上下左右 180度旋轉(zhuǎn)的示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)圖片旋轉(zhuǎn) 上下左右 180度旋轉(zhuǎn)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python3.0 實(shí)現(xiàn)決策樹(shù)算法的流程
這篇文章主要介紹了Python3.0 實(shí)現(xiàn)決策樹(shù)算法的流程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python基礎(chǔ)之函數(shù)與控制語(yǔ)句
在調(diào)用函數(shù)的時(shí)候,如果沒(méi)有按照形參傳入指定的參數(shù),就會(huì)報(bào)錯(cuò),這時(shí),我們可以為函數(shù)的參數(shù)設(shè)置默認(rèn)的值,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)之函數(shù)與控制語(yǔ)句的相關(guān)資料,需要的朋友可以參考下2022-04-04
使用 Python 創(chuàng)建一個(gè)基于規(guī)則的聊天機(jī)器人
這篇文章主要介紹了使用 Python 創(chuàng)建一個(gè)基于規(guī)則的聊天機(jī)器人,使用 Python 創(chuàng)建一個(gè)簡(jiǎn)單的基于規(guī)則的聊天機(jī)器人 聊天機(jī)器人本身是一種機(jī)器或軟件,它通過(guò)文本或句子模仿人類(lèi)交互。 簡(jiǎn)而言之,可以使用類(lèi)似于與人類(lèi)對(duì)話的軟件進(jìn)行聊天。2021-10-10
django admin添加數(shù)據(jù)自動(dòng)記錄user到表中的實(shí)現(xiàn)方法
下面小編就為大家分享一篇django admin添加數(shù)據(jù)自動(dòng)記錄user到表中的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

