python一套完整的事务操作
#coding=utf-8
import sys
import MySQLdb class TransferMoney(object):
def __init__(self,conn):
self.conn = conn #检查账户是否合法
def check_acct_avaiable(self,acctid):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid=%s" % acctid
cursor.execute(sql)
print "check account:" + sql
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("account %s illega" % acctid)
finally:
cursor.close() #检查是否有足够的钱
def has_enough_money(self,acctid,money):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid=%s and money > %s" % (acctid,money)
cursor.execute(sql)
print "has enough money:" + sql
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("account %s not enough money" % acctid)
finally:
cursor.close() #账户减钱
def reduce_money(self,acctid,money):
cursor = self.conn.cursor()
try:
sql = "update account set money = money-%s where acctid = %s" % (money,acctid)
cursor.execute(sql)
print "reduce_money:" + sql
if cursor.rowcount != 1:
raise Exception("reduce money fail %s" % acctid)
finally:
cursor.close() #账户加钱
def add_money(self,acctid,money):
cursor = self.conn.cursor()
try:
sql = "update account set money = money+%s where acctid = %s" % (money,acctid)
cursor.execute(sql)
print "add_money:" + sql
if cursor.rowcount != 1:
raise Exception("add money fail %s" % acctid)
finally:
cursor.close()
#主执行语句
def transfer(self,source_acctid,target_acctid,money):
try:
self.check_acct_avaiable(source_acctid)
self.check_acct_avaiable(target_acctid)
self.has_enough_money(source_acctid,money)
self.reduce_money(source_acctid,money)
self.add_money(target_acctid,money)
self.conn.commit()
except Exception as e:
self.conn.rollback()
raise e if __name__ == "__main__":
source_acctid = sys.argv[1]
target_acctid = sys.argv[2]
money = sys.argv[3]
conn = MySQLdb.Connect(host = '127.0.0.1',port=3306,user='root',passwd='',db='test',charset='utf8')
tr_money = TransferMoney(conn) try:
tr_money.transfer(source_acctid,target_acctid,money)
except Exception as e:
print "Happen:" + str(e)
finally:
conn.close()
python一套完整的事务操作的更多相关文章
- python实现redis三种cas事务操作
cas全称是compare and set,是一种典型的事务操作. 简单的说,事务就是为了存取数据库中同一数据时不破坏操作的隔离性和原子性,从而保证数据的一致性. 一般数据库,比如MySql是如何保证 ...
- python借助ADB工具实现自动化操作手机
核心工具——ADB工具 adb工具用于连接Android手机和PC端,我们借助adb工具,就可以通过命令行对手机进行相应的操作 注意:若要通过adb操作手机,需打开手机的开发者模式,并打开USB调试功 ...
- Python之套接字
Python之套接字 客户端Client/服务端Server架构: 1.硬件C/S架构 2.软件C/S架构 OSI4层:4层里有五层,五层里又有7层. 四层---------五层--------七层 ...
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- Django 事务操作
如何在Django中进行事务操作 案例: 客户A要给客户B转一笔钱,这个在数据库中需要进行两步: 1.客户A减钱 2.客户B加钱 如果在第一步结束后,服务器出现异常,停下了,第二步没有进行,如果数据库 ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- python---django中orm的使用(5)数据库的基本操作(性能相关:select_related,和prefetch_related重点)(以及事务操作)
################################################################## # PUBLIC METHODS THAT ALTER ATTRI ...
- spring(三) spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- MySQL(存储过程,支持事务操作)
day61 保存在MySQL上的一个别名 > 一坨SQL语句 -- delimiter // -- create procedure p1() -- BEGIN -- select * ...
随机推荐
- [golang]反射的用处--代码自动生成
背景: go语言处理db.json的时候,具体代码的变量定义和db字段.json输出的时候可能不一样. 这个时候,我们需要用tag的方式来进行定义. 例如: type MyStruct struct ...
- 根据显示的字符多少来做Label的自适应高度
根据显示的字符多少来做Label的自适应高度 UILabel *label = [[UILabel alloc]init]; NSString *string = @"其实,经年过往,每个人 ...
- POJ 1065 Wooden Sticks Greed,DP
排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- 20145225《Java程序设计》 第9周学习总结
20145225<Java程序设计> 第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1JDBC JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂 ...
- 数据模型类对比 反射c#
using System;using System.ComponentModel.DataAnnotations; public class LoginModel { ...
- ADB工具常用指令和使用情形分析
一.ADB常用命令:(不需要进到设备,也就是:不需要执行adb shell) adb devices 列出所有的已连接的设备 adb kill-server 关闭adb adb start-serve ...
- Jade之Template Inheritance
Template inheritance jade支持通过关键字block和extends来实现模板继承. 比如,在layout.jade写上如下代码 html head title My Site ...
- iOS 设置navigationBar背景
- (void)viewWillAppear:(BOOL)animated { [superviewWillAppear:animated]; [self.navigationContro ...
- How to configure windows machine to allow file sharing with dns alias (CNAME)
Source: http://serverfault.com/questions/23823/how-to-configure-windows-machine-to-allow-file-sharin ...
- hdu 5101 n集合选2个不同集合数使和大于k
http://acm.hdu.edu.cn/showproblem.php?pid=5101 给n个集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案. 答案=从所有数中选择的两个加和大于 ...