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 ...
随机推荐
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- z-index的最大值、最小值
浏览器 最大值 超过最大值后变成 最小值 小于最小值后变成 备注 IE6 2147483647 2147483647 -2147483648 -2147483648 IE7 2147483647 ...
- 如何获取网站icon
获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon(<link rel="shortcut ico ...
- Go http.HandlerFunc()
//http.Handler type Handler interface { ServeHTTP(ResponseWriter, *Request) } type HandlerFunc func( ...
- ural 1104 Don’t Ask Woman about Her Age
http://acm.timus.ru/problem.aspx?space=1&num=1104 #include <cstdio> #include <cstring&g ...
- ural 1572 Yekaterinozavodsk Great Well
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...
- java疯狂演义----简单java IDE工具
file:commons package org.crazyit.editor.commons; import org.crazyit.editor.EditorFrame; import org.c ...
- USB 0xC0000012 错误详解
0xC0000012Bus Hound 6.01 capture on Windows Vista Service Pack 1 (x86). Complements of www.perisoft. ...
- 【转】ArrayList遍历的同时删除----不错
原文网址:http://javag.iteye.com/blog/403097 方法一 ArrayList<String> list = new ArrayList<String&g ...
- CF-Mr. Kitayuta's Colorful Graph
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...