Get the App
SLTechnology News&Howtos  ›  Development  › 

How to insert data using front-end pages in SQLAlchemy in python

Shulou Source: shulou.com Published: 2022-06-01 03:44:58 12月02日 Update

This article mainly introduces the "python SQLAlchemy how to use the front-end page to insert data" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to use the front-end page in python to insert data" article can help you solve the problem.

1. Experimental effect

If the inserted data already exists in the database, the following prompt appears:

Look at the data in the database table and find that the data has been stored in the database table:

two。 Main main.py file import osfrom flask_sqlalchemy import SQLAlchemyfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy import String,Integer,create_engine,Columnfrom flask import Flask,render_template,redirect,request,url_for,abort,jsonifyapp=Flask (_ _ name__) class Config: "" related configuration "" # cmd: # create database: create database flaskdb (database name) default charset (type) utf8; # use data: use flaskdb # View database tables: show tables SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:root@127.0.0.1:3306/flaskdb' SQLALCHEMY_TRACK_MODIFICATIONS=Trueapp.config.from_object (Config) # create database mysql=SQLAlchemy (app) # create table class Moster (mysql.Model): "administrator table name" _ tablename__='moster' username=Column (String, primary_key=True) password=Column (String, unique=True) @ app.route ('/ /', methods= ['POST') 'GET']) def Insert_User (username,password): # determine whether this user already exists in the database table If it exists, do not insert data data=Moster.query.filter (Moster.username==username) .all () if data== []: # create an object Insert data mos = Moster (username=username, password=password) # create session mysql.session.add (mos) mysql.session.commit () # close the database mysql.session.close () return jsonify ('Add the data Successeded') Else: return jsonify ('The data have been existedboys') @ 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') # clear the database Make the database "clean" # mysql.drop_all () # create table mysql.create_all () app.run (debug=True) 3. Front-end mysql.html file MySQL 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;} account:

Password:

This is the end of the introduction on "how to insert data using the front-end page in SQLAlchemy in python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Data database front end page knowledge file industry different practical clean content practical actual password object effect article method more case Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei macOS Apple Docker