網(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
相關(guān)推薦
- 2023-01-21 VmWare安裝Centos后配置Net網(wǎng)絡(luò)SSH鏈接問(wèn)題及解決_VMware
- 2022-12-06 Python中八種數(shù)據(jù)導(dǎo)入方法總結(jié)_python
- 2022-07-30 Redis全局ID生成器的實(shí)現(xiàn)_Redis
- 2022-07-30 if-else和switch的練習(xí)及區(qū)別比較
- 2022-07-01 事務(wù)在c#中的使用_C#教程
- 2022-12-08 C#與C++?dll之間傳遞字符串string?wchar_t*?char*?IntPtr問(wèn)題_C#
- 2022-07-15 python新手練習(xí)實(shí)例之萬(wàn)年歷_python
- 2022-07-18 SQL?Server使用T-SQL進(jìn)階之公用表表達(dá)式(CTE)_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支