网站首页 > 文章精选 正文
一、应用场景
- Python项目对MySQL数据库进行增、删、改操作时,有时会出现执行sql异常的情况。在批量提交数据的时候,如果其中一个事务提交错误,往往导致预期的整个数据链不完整。
- 例如银行转账数据,用户A向用户B转账,
步骤一:此时A的数据要存一份某个时间向B转账的数据。
步骤二:同时用户B也会存一份某个时间收到A转账的数据。
如果此时步骤一执行sql成功,步骤二出错并且未回滚事务,将会导致两边备份的数据校对错误。当引入事务时,步骤二出现错误,事务回滚后步骤一未执行,数据回归到出错前的情况,保证了数据的安全性。
二、使用方法
- 配合我们之前文章提到的try的使用(Python之异常处理(try的基本用法)),还有上篇文章学到的mysql的基本操作(Python MySQL数据库连接)实现捕捉异常并且回滚。
try:
cursor.execute(sql_1)
cursor.execute(sql_2)
cursor.execute(sql_3)
except Exception as e:
# 事务回滚
connect.rollback()
print('事务处理失败', e)
else:
# 事务提交
connect.commit()
print('事务处理成功', cursor.rowcount)
三、测试demo
- 事务出现错误demo
# -*- coding: utf-8 -*-
"""
@Time : 2022/1/27 17:34
@Auth : 技术空间
@File :rollback_demo.py
@IDE :PyCharm
@Motto:技术总是要日积月累的
"""
import pymysql
if __name__ == '__main__':
db = pymysql.connect(host='localhost',
user='root',
password='root',
database='others')
cursor = db.cursor(pymysql.cursors.DictCursor)
select_sql = " select id,name from user_info "
cursor.execute(select_sql)
data_list = cursor.fetchall()
print("执行更新前的数据-->")
print(data_list)
try:
update_sql1 = " update user_info set name='1我的头条号:code_space' where id = 1 "
update_sql2 = " update user_info set name='2我的头条号:code_space' where id = 2 "
# 数据库的name长度是20,这里长度超过了,会报Data too long错误
update_sql3 = " update user_info set name='测试测试测试测试测试测试测试测试测试测试测试测试' where id = 3 "
cursor.execute(update_sql1)
cursor.execute(update_sql2)
cursor.execute(update_sql3)
except Exception as e:
# 事务回滚
db.rollback()
print('事务处理失败', e)
else:
# 事务提交
db.commit()
print('事务处理成功', cursor.rowcount)
cursor.execute(select_sql)
data_list = cursor.fetchall()
print("执行更新后的数据-->")
print(data_list)
cursor.close()
db.close()
- 事务正确执行demo
# -*- coding: utf-8 -*-
"""
@Time : 2022/1/27 17:34
@Auth : 技术空间
@File :rollback_demo.py
@IDE :PyCharm
@Motto:技术总是要日积月累的
"""
import pymysql
if __name__ == '__main__':
db = pymysql.connect(host='localhost',
user='root',
password='root',
database='others')
cursor = db.cursor(pymysql.cursors.DictCursor)
select_sql = " select id,name from user_info "
cursor.execute(select_sql)
data_list = cursor.fetchall()
print("执行更新前的数据-->")
print(data_list)
try:
update_sql1 = " update user_info set name='1我的头条号:code_space' where id = 1 "
update_sql2 = " update user_info set name='2我的头条号:code_space' where id = 2 "
update_sql3 = " update user_info set name='3我的头条号:code_space' where id = 3 "
cursor.execute(update_sql1)
cursor.execute(update_sql2)
cursor.execute(update_sql3)
except Exception as e:
# 事务回滚
db.rollback()
print('事务处理失败', e)
else:
# 事务提交
db.commit()
print('事务处理成功', cursor.rowcount)
cursor.execute(select_sql)
data_list = cursor.fetchall()
print("执行更新后的数据-->")
print(data_list)
cursor.close()
db.close()
四、拓展
在处理事务过程中,往往涉及到批量执行sql语句的情况,比如批量提交订单、批量删除订单等。下篇文章我们将会讲解关于批量执行sql语句的代码,涉及到代码复用的重要操作。还请多多关注。
关注我,坚持每日积累一个技巧,长期坚持,我们将会不断进步。
猜你喜欢
- 2024-12-23 python网络爬虫:批量爬取图片 python批量爬取图片并保存
- 2024-12-23 10w qps缓存数据库——Redis redis缓存数据量多大开始性能下降
- 2024-12-23 Python File(文件) 常用场景 python中file.write
- 2024-12-23 「JS 逆向百例」某网站加速乐 Cookie 混淆逆向详解
- 2024-12-23 「JS 逆向百例」猿人学web比赛第五题:js 混淆 - 乱码增强,详细剖析
- 2024-12-23 python 大量乱序文件如何合并成有序的
- 2024-12-23 如何使用 PyScript 在 Web 浏览器上轻松运行
- 2024-12-23 Selenium4.0+Python3系列(四) - 常见元素操作(含鼠标键盘事件)
- 2024-12-23 Selenium4+Python3系列(六) - 强制等待、隐式等待、显式等待
- 2024-12-23 Python文件、文件夹删除之os、shutil
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)