当前位置:网站首页>[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()
边栏推荐
- 全力冲unreal了
- Matlb| optimal operation and marketization of power system
- MCU modifies network hardware driver (PHY chip replacement)
- 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
- 2022危险化学品生产单位安全生产管理人员复习题及答案
- Dnslog injection
- Can tongdaxin open an account for stock trading? Is it safe?
- Applet graduation project is based on wechat property maintenance application property applet graduation project opening report function reference
- 使用Pega进行一个简单的RPA程序开发
- Google推出Advanced API Security 保护API免受安全威胁
猜你喜欢
随机推荐
Ask the bosses why the number type of Oracle CDC becomes a string when printed out. How can it be converted back?
为什么 insert 配置 'SELECT LAST_INSERT_ID()' 返回个0呢?
TDengine&nbsp; × Intel edge insight software package accelerates the digital transformation of traditional industries
Matlb| visual learning (plot and bar)
Does rapid software delivery really need to be at the cost of security?
July2022 plan (unreal)
强化学习在黄页商家智能聊天助手中的探索实践
CSDN Blogger
The fourth largest operator cannot be a "catfish"
Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
win10用cmake3.22与vs2019编译curl库源码并调用
2022年化工自动化控制仪表考试模拟100题模拟考试平台操作
Squid proxy server application (I came from afar to make an appointment with you)
Advanced C language
How to create a CSR (certificate signing request) file?
324. 摆动排序 II
Would you like to ask for advice on how to open a stock account? Is it safe to open an account online?
剑指 Offer 11. 旋转数组的最小数字
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
[tcapulusdb knowledge base] modify business modify cluster







![[tcapulusdb] I wish you all a healthy Dragon Boat Festival!](/img/82/2357f9d195cfbb38c4052790804a6f.png)

