当前位置:网站首页>[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()
边栏推荐
- DMS的SQL结果集导出支持传参数吗?
- Architecture autonomy service: building data-driven architecture insight
- 抓包整理外篇fiddler————了解工具栏[一]
- [algorithm] I brushed two big factory interview questions and learned array again with tears in my eyes“
- 6 - Dictionary
- Nuc980 heartbeat light
- TDengine&nbsp;×英特尔 边缘洞见软件包 加速传统行业的数字化转型
- 如何制作CSR(Certificate Signing Request)文件?
- Common DOS commands
- Does the dataworks SQL script support if else judgment of statement blocks
猜你喜欢

Matlb| optimal operation and marketization of power system

使用Pega进行一个简单的RPA程序开发

Exploration and practice of reinforcement learning in yellow page merchants' intelligent chat assistant

The fourth column, kubernetes cloud native combat, is coming ~
![Halcon knowledge: matrix topic [01]](/img/d3/2726da70c41c7adccc3474be6f51c8.png)
Halcon knowledge: matrix topic [01]

面部識別試驗涉及隱私安全問題?國外一公司被緊急叫停

Visio use

Google launches advanced API security to protect APIs from security threats

Flutter 小技巧之 MediaQuery 和 build 优化你不知道的秘密

Why insert is configured with'select last_ INSERT_ What if id() 'returns 0?
随机推荐
居家高效远程办公 | 社区征文
C 语言进阶
Small program graduation project based on wechat campus lost and found graduation project opening report function reference
TDengine&nbsp;×英特尔 边缘洞见软件包 加速传统行业的数字化转型
请问大智慧上开户安全吗
Redis 原理 - Hash
Small program graduation project based on wechat milk tea takeout mall small program graduation project opening report function reference
数据库MySQL语句期末复习 CTGU
Database mysql statement final review CTGU
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
dataworks SQL脚本支持语句块的if else 判断吗
Large scale sports events and crime risks
Small program graduation project based on wechat recruitment small program graduation project opening report function reference
Introduction to kubernetes resource object and common commands
2022 practice questions and mock examination of Shandong Province safety officer C certificate examination
Does rapid software delivery really need to be at the cost of security?
麻醉不止“打一针”那么简单,无痛、安全、舒适为患者首选
Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
如何制作CSR(Certificate Signing Request)文件?
全力冲unreal了