[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>概念
事务:是一套完整的业务逻辑,在这套业务逻辑中可能包含多天SQL语句,这些SQL语句要么都成功,要么都失败
举例:张三给李四转账100元。
<>特征
原子性(Atomicity): 事务中包含的操作被看做一个逻辑单元,这个逻辑单元中的操作要么全部成功,要么全部失败
一致性(Consistency):事务的结果保留不变,即事务的运行并不改变数据的一致性
隔离性(Isolation):又称孤立性,事务的中间状态对其它事务是不可见的
持久性(Durability):指一个事务一旦提交成功,它对数据库中数据的改变就应该是永久性的
<>事务提交机制
* 概念
提交: 将内存中的修改写入数据库
回滚: 拒绝将内存中的修改写入数据库
* 方式
提交:
方式1: 手动提交 连接对象.commit()
方式2: 自动提交 创建连接 autocommit=True
回滚:
方式1: 默认情况下,在 pymysql 不提交就回滚
方式2: 手动回滚 连接对象.rollback()
<>事务操作
需求:先往 t_book 表插入书的信息,然后向 t_hero 插入该书主角的信息,要求,要么都成功,要么都失败
核心知识点: 事务的实现(通用模板)
事务操作需要使用try控制分支使用if else不可以
try: sql1 = "xxxx" cursor.execute(sql1) sql2 = "yyy" cursor.execute(sql2) conn.
commit() # 事务提交 except Exception as e: conn.rollback() # 事务回滚 finally: # 5.释放资源
cursor.close() conn.close()