学习中, 本人为初学者。勿喷。

#-*- coding:utf-8 -*-
import MySQLdb
class Tranferaccount(object):
def __init__(self,sqlcon):
self.sqlcon = sqlcon
def account_check_avaiable(self,accid):
cursor = self.sqlcon.cursor()
sql_str = "select * from account where accid = %s "% accid
try:
print 'account_check_avaiable:%s'%sql_str
cursor.execute(sql_str)
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception('此账号%s不存在'%accid)
finally:
cursor.close()
def account_enough_money(self,accid,money):
cursor = self.sqlcon.cursor()
sql_str = "select * from account where accid = %s and money >%s "% (accid,money)
try:
print 'account_enough_money:%s'%sql_str
cursor.execute(sql_str)
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception('此账号%s余额不足'%accid)
finally:
cursor.close()
def account_reduce_money(self,accid,money):
cursor = self.sqlcon.cursor()
sql_str = "update account set money = money-%s where accid = %s "% (money,accid)
try:
print 'account_reduce_money:%s'%sql_str
cursor.execute(sql_str)
if cursor.rowcount != 1:
raise Exception('此账号%s减款失败'%accidd)
finally:
cursor.close()
def account_add_money(self,accid,money):
cursor = self.sqlcon.cursor()
sql_str = "update account set money = money+%s where accid = %s "% (money,accid)
try:
print 'account_add_money:%s'%sql_str
cursor.execute(sql_str)
if cursor.rowcount != 1:
raise Exception('此账号%s加款失败'%accid)
finally:
cursor.close()
def tranfer(self,source_accid,dest_accid,money):
try:
self.account_check_avaiable(source_accid)
self.account_check_avaiable(dest_accid)
self.account_enough_money(source_accid,money)
self.account_reduce_money(source_accid,money)
self.account_add_money(dest_accid,money)
self.sqlcon.commit()
except Exception as e:
self.sqlcon.rollback()
raise e
if __name__ == "__main__":
source_accid = 11
dest_accid = 12
money = 100
conn = MySQLdb.connect(host='127.0.0.1',user='root',db = 'liunx',passwd='root',port=3306,charset='utf8')
tr_money = Tranferaccount(conn)
try:
tr_money.tranfer(source_accid,dest_accid,money)
except Exception as e:
print '出现问题:%s'% e
finally:
conn.close()

mysql for python,银行转账模拟的更多相关文章

  1. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  2. 在Ubuntu上安装Mysql For Python

    安装: 首先安装pip,并且把pip更新到最小版本 apt-get install python-pip pip install -U pip 安装mysql开发包 apt-get install p ...

  3. Snippet: Fetching results after calling stored procedures using MySQL Connector/Python

    https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Pytho ...

  4. How to Access MySQL with Python Version 3.4

    http://askubuntu.com/questions/630728/how-to-access-mysql-with-python-version-3-4 How to Access MySQ ...

  5. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  6. python urllib2 模拟网站登陆

    python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...

  7. Python实现模拟登陆

    大家经常会用Python进行数据挖掘的说,但是有些网站是需要登陆才能看到内容的,那怎么用Python实现模拟登陆呢?其实网路上关于这方面的描述很多,不过前些日子遇到了一个需要cookie才能登陆的网站 ...

  8. Python中模拟enum枚举类型的5种方法分享

    这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下   以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...

  9. Installing MySQL Connector/Python using pip v1.5

    The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...

随机推荐

  1. 1像素HR技巧(兼容各浏览器)

    hr{color:#ccc;height:1px;border:0px;border-top:1px solid #ccc;margin:0px;padding:0px;overflow:hidden ...

  2. Android 解决服务端验证码问题

    服务端验证码解决方法. 在服务端生成验证码后会把验证码字符串存在服务端的session中,等待用户提交进行比对.为了保证服务器与客户端的一对一的关系,所以出现了session 和cookie技术.客户 ...

  3. android ids.xml资源的使用

    ids.xml文件例子: XML file saved at res/values/ids.xml: 使用方式: 一:

  4. BZOJ3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆

    3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 11  So ...

  5. Linux系统编程(27)——线程控制

    进程在各自独立的地址空间中运行,进程之间共享数据需要用mmap或者进程间通信机制,那么如何在一个进程的地址空间中执行多个线程呢.有些情况需要在一个进程中同时执行多个控制流程,这时候线程就派上了用场,比 ...

  6. City Tour

    Description Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B.不过Alice很讨厌坐火车,火车上人比 ...

  7. HDU_2041——走楼梯,递推

    Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?   Input 输入数据首先包含一个整数N,表示测试实例的个数,然 ...

  8. HDOJ 2191

    多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...

  9. hadoop2.2.0 MapReduce的序列化

    package com.my.hadoop.mapreduce.dataformat; import java.io.DataInput;import java.io.DataOutput;impor ...

  10. 读取xml到DataSet中去

    XML如下: <?xml version="1.0" encoding="utf-8" ?> <Config> <System&g ...