当前位置:网站首页>[flask] update and delete crud of data
[flask] update and delete crud of data
2022-06-28 18:15:00 【YZL40514131】
Premise : Need to generate data table
The following steps 1 to 6 are the operation steps for generating data tables
First step : Database configuration
# Configuration variables of the database
HOSTNAME = '127.0.0.1'
PORT = '3306'
DATABASE = 'test'
USERNAME = 'root'
PASSWORD = 'root'
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)
The second step : Create database engine
engine=create_engine(DB_URI)
The third step : Test whether the data connection is successful
with engine.connect() as conn:
res=conn.execute('select 1')
print(res.fetchone())
Step four : Create base class
Base=declarative_base(engine)
Step five : Create model classes
class Person(Base):
__tablename__='t_person' # Create table name , It is best to t_ start
id=Column(name='id',type_=Integer,primary_key=True,autoincrement=True)
name=Column(name='name',type_=String(255))
age=Column(name='age',type_=Integer)
address=Column(name='address',type_=String(255))
country=Column(name='country',type_=String(50)) # New fields added after the table is created
city=Column(name='city',type_=String(50)) # New fields added after the table is created
def __str__(self):
return ' full name :{}- Annual collar :{}- Address :{}- Country :{}- City :{}'.format(self.name,self.age,self.address,self.country,self.city)
Step six : Create a data table based on the model class
#Base.metadata.drop_all()
Base.metadata.create_all()
Step seven : establish session object
session=sessionmaker(engine)() # Be careful sessionmaker Returns a function
One 、 Modify the data
First, find the object from the database , Then modify the data to the data you want , Do it last commit The data can be modified by operation .
give an example 1:
Name it zz The characters , Change the name to jmeter
p1=session.query(Person).filter(Person.name=='zz').first()
p1.name='jmeter'
session.commit()
give an example 2:
Name all names as zz The characters , Change the name to jmeter
res=session.query(Person).filter(Person.name=='zz').all()
print(res)
for i in res:
i.name='flask'
session.commit()
Two 、 Delete data
First, find the object from the database , Then delete this data , Do it last commit Then you can delete the data .
p1=session.query(Person).filter(Person.name=='kb').first()
session.delete(p1)
session.commit()
Particular attention :p1=session.query(Person).filter(Person.name=='kb').first()p1 It's the object , You can delete ;p2=session.query(Person).filter(Person.name=='kb').all()p2 It's a list object , You cannot delete a list object , If you want to delete a list object , adopt for Cyclic operation
The code is as follows
def delete():
res1 = session.query(Person).filter(Person.sal <= 3000).all()
print(res1)
for i in res1:
session.delete(i)
session.commit()
边栏推荐
- CSDN Blogger
- kubeadm创建kubernetes集群
- 使用Pega进行一个简单的RPA程序开发
- 9个优秀的占位图服务
- MySQL十种锁,一篇文章带你全解析
- 【译】clickhouse 22.4和22.5核心特性一览
- 6 - Dictionary
- 2022 practice questions and mock examination of Shandong Province safety officer C certificate examination
- Redis principle - hash
- Redis6 notes 04 master-slave replication, cluster, application problems, new redis6 features
猜你喜欢

2022年山东省安全员C证考试练习题及模拟考试

Small program graduation project based on wechat milk tea takeout mall small program graduation project opening report function reference

The fourth column, kubernetes cloud native combat, is coming ~

The fourth largest operator cannot be a "catfish"

Anesthesia is not as simple as "one injection". Painless, safe and comfortable anesthesia is the first choice for patients

It's said that software testing for career change can only be self-taught. Is the training institution a pit?

2022 operation of simulated examination platform of hoisting machinery command examination question bank

2022危险化学品生产单位安全生产管理人员复习题及答案

Flutter tips: mediaquery and build optimization secrets you don't know

Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
随机推荐
How to configure the Oracle CDC service name without Sid?
Squid proxy server application (I came from afar to make an appointment with you)
How much is the data delay when you collect Oracle data? I can't keep it down for 3 seconds. Is there an industry reference
Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference
kubeadm创建kubernetes集群
Pure big resentment! Those who were discouraged from taking the postgraduate entrance examination
Ten MySQL locks, one article will give you full analysis
Redis 原理 - Hash
[official cooperation between Shang Silicon Valley and Tencent cloud] video release of Silicon Valley classroom project
数据库实验7 完整性约束
Leetcode 6. Z 字形变换(牛逼,解决了)
2022年山东省安全员C证考试练习题及模拟考试
为什么 insert 配置 'SELECT LAST_INSERT_ID()' 返回个0呢?
July2022 plan (unreal)
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
The most complete 360 software test case template without dead corners in the whole network [recommended collection]
Idea of capturing mobile terminal variant combination
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
2022 operation of simulated examination platform of hoisting machinery command examination question bank
How to upgrade from RHEL 8 to RHEL 9