一、事务处理
1、为了避免因意外产生不必要的损失,当语句集全部执行成功commit、执行失败rollback,
2、事务处理不是绑定在sql服务器,而是与sql引擎有关,只有innoDB和BDB支持事务,
二、mysql的事务处理
-- 支持事务的存储过程 delimiter $$ drop procedure if EXISTS proc_rollback $$ create procedure proc_rollback(inout p_return_code tinyint) begin -- error declare exit handler for sqlexception rollback; -- warning declare exit handler for sqlwarning rollback; -- start start transaction; update woman set asset=asset-5 where name='luna'; update woman set asset=asset+5 where name='haha'; -- success commit; set p_return_code=1; end $$ delimiter ; -- call procedure set @n=0; call proc_rollback(@n); select @n;
三、pymysql的事务处理
pymysql默认支持事务处理,当语句集未全部执行通过时直接回滚,
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='',db='chouti',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("update woman set asset=asset-5 where name='luna'") cursor.execute("update woman set asset=asset+5 where name='haha'") conn.commit() cursor.close() conn.close()
浙公网安备 33010602011771号