python中SQLAlchemy使用前端頁面實(shí)現(xiàn)插入數(shù)據(jù)
1.實(shí)驗(yàn)效果
如果插入的數(shù)據(jù)已經(jīng)存在于數(shù)據(jù)庫中,則出現(xiàn)以下提示:
查看數(shù)據(jù)庫表中的數(shù)據(jù),發(fā)現(xiàn)已經(jīng)將數(shù)據(jù)存入了數(shù)據(jù)庫表中:
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ù)庫:create database flaskdb(數(shù)據(jù)庫名) default charset(類型) utf8; ? ? # 使用數(shù)據(jù):use flaskdb ? ? # 查看數(shù)據(jù)庫表: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ù)庫 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ù)庫表中是否已經(jīng)存在了此用戶,如果存在,則不進(jìn)行插入數(shù)據(jù) ? ? data=Moster.query.filter(Moster.username==username).all() ? ? if data==[]: ? ? ? ? # 創(chuàng)建對象,進(jìn)行數(shù)據(jù)的插入 ? ? ? ? mos = Moster(username=username, password=password) ? ? ? ? # 創(chuàng)建session ? ? ? ? mysql.session.add(mos) ? ? ? ? mysql.session.commit() ? ? ? ? # 關(guān)閉數(shù)據(jù)庫 ? ? ? ? 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') ? ? # 對數(shù)據(jù)庫進(jìn)行清除,讓數(shù)據(jù)庫是“干凈的” ? ? # 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>
到此這篇關(guān)于SQLAlchemy使用前端頁面實(shí)現(xiàn)插入數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SQLAlchemy插入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中l(wèi)ist列表復(fù)制的幾種方法(賦值、切片、copy(),deepcopy())
本文主要介紹了python中l(wèi)ist列表復(fù)制的幾種方法(賦值、切片、copy(),deepcopy()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python深度強(qiáng)化學(xué)習(xí)之DQN算法原理詳解
DQN算法是DeepMind團(tuán)隊(duì)提出的一種深度強(qiáng)化學(xué)習(xí)算法,在許多電動游戲中達(dá)到人類玩家甚至超越人類玩家的水準(zhǔn),本文就帶領(lǐng)大家了解一下這個(gè)算法,快來跟隨小編學(xué)習(xí)一下2021-12-12Python實(shí)戰(zhàn)之能監(jiān)控文件變化的神器—看門狗
這篇文章主要介紹了Python實(shí)戰(zhàn)之能監(jiān)控文件變化的神器—看門狗,文中有非常詳細(xì)的圖文及代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05python實(shí)現(xiàn)簡單的單變量線性回歸方法
今天小編就為大家分享一篇python實(shí)現(xiàn)簡單的單變量線性回歸方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python實(shí)現(xiàn)異常值自動檢測的案例分享
在數(shù)據(jù)分析和機(jī)器學(xué)習(xí)中,異常值的檢測是一個(gè)關(guān)鍵步驟,它有助于識別數(shù)據(jù)中的異常模式和離群點(diǎn),本文將介紹Python中異常值檢測的實(shí)戰(zhàn)案例,使用一些常見的技術(shù)和庫,為大家提供全面的示例代碼和詳細(xì)解釋2024-01-01一文帶你深入理解python中pytest-repeat插件的工作原理
這篇文章主要和大家一起深入探討到底pytest_repeat插件的具體功能是如何實(shí)現(xiàn)的呢,相信具體了解了該插件,其他三方插件也可以很快了解它內(nèi)部運(yùn)行機(jī)制,所以本文詳細(xì)講解了python pytest-repeat插件的工作原理,需要的朋友可以參考下2023-09-09