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

#-*- 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. 一个正整数N,拆成任意个正整数之和,怎样使这些数的乘积最大

    网上看到了如标题所示的题目,就开始想如果用程序来算的话,那么它的算法是怎样的. 自己想了半天,第一感觉要用递归, 如先算出 当 n=1 max=1 当 n=2 max=1 当 n=3 max=2 .. ...

  2. [布局] bootstrap基本标签总结

    文件头: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <titl ...

  3. WINDOWS基本数据类型示例

    最近也学学这些,争取把所有东东都串起来. #include <Windows.h> #include <stdio.h> int WINAPI WinMain( HINSTAN ...

  4. C++代码覆盖率工具Coverage Validator

    市面上的C++代码覆盖率工具大都收费,Coverage Validator也不例外.Coverage Validator应该少有人听过,我也是在stackoverflow里听别人介绍的.所以下载了试用 ...

  5. Leetcode:Largest Number详细题解

    题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...

  6. rsyslog 读取单个文件测试

    rsyslog 测试(rsyslog 必须yum 安装uat-web02:/root# rpm -qa | grep rsyslog rsyslog-8.21.0-1.el6.x86_64) //读取 ...

  7. Cookie和Session(转)

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  8. 创建git repo

    http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository Getting a Git Repository You can get ...

  9. window.open的小技巧分享

        今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明.       聊到window.open,不得不说明一下他的使用方法,主要有两种形式:   win ...

  10. jQuery简单的Ajax调用示例

    jQuery确实方便,下面做个简单的Ajax调用: 建立一个简单的html文件: <!DOCTYPE HTML> <html> <head> <script ...