Python实现完整的事务操作示例

作者:北京流浪儿 时间:2023-11-15 21:02:02 

本文实例讲述了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,事务
0
投稿

猜你喜欢

  • 强制SQL Server执行计划使用并行提升在复杂查询语句下的性能

    2024-01-14 16:30:38
  • python 读取.csv文件数据到数组(矩阵)的实例讲解

    2023-08-10 12:12:36
  • 细化解析:Mysql数据库对文件操作的封装

    2008-11-27 16:32:00
  • Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】

    2023-05-21 01:32:42
  • python 读txt文件,按‘,’分割每行数据操作

    2022-11-11 08:04:17
  • Facebook的特别之处是什么?

    2008-08-04 12:57:00
  • python如何将自己的包上传到PyPi并可通过pip安装的方法步骤

    2021-04-02 10:53:07
  • 改进SQL Server数据库系统安全五步走

    2009-01-20 11:47:00
  • 通过实例解析Python return运行原理

    2021-08-11 18:31:31
  • 详解利用Python scipy.signal.filtfilt() 实现信号滤波

    2022-09-23 21:23:03
  • Python异常之常见的Bug类型解决方法

    2022-09-02 15:17:14
  • Python request操作步骤及代码实例

    2022-03-16 10:42:47
  • python 获取文件列表(或是目录例表)

    2021-06-27 20:14:24
  • Pandas创建DataFrame提示:type object 'object' has no attribute 'dtype'解决方案

    2022-08-06 16:33:18
  • MySQL数据库中应当如何实施info()函数

    2008-11-27 15:04:00
  • JS鼠标事件大全 推荐收藏

    2024-05-28 15:41:00
  • Python使用pandas导入xlsx格式的excel文件内容操作代码

    2022-03-12 04:29:57
  • python ipset管理 增删白名单的方法

    2021-02-10 17:38:19
  • WEB前端开发规范文档

    2010-10-19 12:32:00
  • sqlserver中去除字符串中连续的分隔符的sql语句

    2024-01-24 02:45:51
  • asp之家 网络编程 m.aspxhome.com