pymysql transaction
2020-08-16 20:44
615 查看
# 导包 import pymysql # 捕获异常 conn,cursor = None, None # 提前定义变量,防止出现异常之后,没有变量的问题 try : # 建立连接 conn = pymysql.connect(host="localhost", user="root", password="root", database="books", port=3306, charset="utf8") # 获取游标 cursor = conn.cursor() # 先执行插入三体这本书的操作 cursor.execute("insert into t_book(`id`,`title`,`pub_date`) values(11, '三体', '1970-01-01');") # 再执行插入逻辑人物的操作 cursor.execute("insert into t_hero(id, `name`,`gender`,`book_id`) values('逻辑11111', 1, 4);") # 提交事务 conn.commit() print("提交事务成功") except Exception as e: # 回滚事务 cursor.execute("select * from t_book;") print("回滚之前书中有哪些数据:",cursor.fetchall()) cursor.execute("select * from t_hero;") print("回滚之前英雄人物中有哪些数据:", cursor.fetchall()) conn.rollback() # 回滚 cursor.execute("select * from t_book;") print("回滚之后书中有哪些数据:",cursor.fetchall()) cursor.execute("select * from t_hero;") print("回滚只会英雄人物中有哪些数据:", cursor.fetchall()) print("回滚事务成功") finally: # 关闭游标和关闭连接 cursor.close() conn.close()
相关文章推荐
- LoadRunner专题系列——事务Transaction
- sql server出错-transaction can not start while firehose mode
- Report to Find User-Exit for Transaction Code
- Transaction Event Key in OBYC
- Java Transaction Design Strategies
- 事务处理(Transaction)
- SQL回滚Transaction来调试SQL语句
- Trick: 巧用.NET Reflector, SOS Debugging找出和某一个TransactionScope绑定的SqlConnection objects以及SqlConnection中开着的SqlDataReader objects (Find all SqlConnection objects associated with a TransactionScope)
- Transaction Processing Middleware
- 事务(Transaction)的特性和状态
- 随笔--关于Linq to Sql Transaction
- JTA Error creating bean with name 'transactionManager' defined in file No JTA UserTransaction available - specify either 'user
- Transaction rolled back because it has been marked as rollback-only
- How to identify a session started by a remote distributed transaction
- How Setting Show Transaction Code on SAP Tree Menu Side.
- SQL基础-->数据库事务(TRANSACTION)
- org.hibernate.TransactionException: JDBC rollback failed 处理
- mysql : Lock wait timeout exceeded; try restarting transaction
- RPM 错误 can’t create transaction lock on
- Transaction, Lock, Isolation Level