In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.