mysql for python,银行转账模拟
学习中, 本人为初学者。勿喷。
#-*- 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,银行转账模拟的更多相关文章
- Python requests模拟登录
Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...
- 在Ubuntu上安装Mysql For Python
安装: 首先安装pip,并且把pip更新到最小版本 apt-get install python-pip pip install -U pip 安装mysql开发包 apt-get install p ...
- 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 ...
- 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 ...
- #MySQL for Python(MySQLdb) Note
#MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...
- python urllib2 模拟网站登陆
python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...
- Python实现模拟登陆
大家经常会用Python进行数据挖掘的说,但是有些网站是需要登陆才能看到内容的,那怎么用Python实现模拟登陆呢?其实网路上关于这方面的描述很多,不过前些日子遇到了一个需要cookie才能登陆的网站 ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- 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 ...
随机推荐
- Spark Streaming 数据接收过程
SparkStreaming 源码分析 一节中从源码角度,描述了Streaming执行时代码的调用过程.下边就接收转化阶段过程再简单分析一下,为分析backpressure作准备. SparkStre ...
- 转:QT 的点点滴滴 错误总结
转自:http://blog.csdn.net/lbsljn/archive/2009/12/29/5099590.aspx MinGw + CodeBlock + QT4.5 类定义后面要加&quo ...
- log4net 学习笔记
记入最基本的用法 : refer : http://www.cnblogs.com/aehyok/archive/2013/05/07/3066010.html <configuration&g ...
- VCRedist.exe静默安装方法(转)
INNO setup 制作安装包 的时候,发布VC++运行时 [Run]Filename: {app}vcredist_x86.exe; Parameters: /q; WorkingDir: {t ...
- iOS 9之分屏多任务(Split View)
金田(github 示例源码) 多任务(multitasking)算是iOS9中最引人瞩目的核心新特性了,之前越狱版用户就用过类似的插件,微软的 苏菲 (Windows Surface)系列也有分屏多 ...
- android中操作SQLite注意事项
调用rawQuery()后,需要调用Cursor的movToFirst(); public Cursor query(String sql, String[] args){ SQLiteDatabas ...
- MapReduce优化一(改变切片大小和Shuffle过程Reduce占用堆大小)
/*为防止处理超大作业时超时,将io时间设为1小时 * <property> <name>dfs.datanode.soc ...
- JS(五)
感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...
- Showing 2 changed files with 3 additions and 3 deletions.
4 lib/matplotlib/__init__.py View @@ -126,9 +126,9 @@ def compare_versions(a, b): else: ...
- String 和 InputStream 互转方式
/** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...