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 ...
随机推荐
- Android onActivityResult 设置requestCode 返回的code不对
今天在项目里用到 Intent intent=new Intent(getActivity(), Test.class);startActivityForResult(intent, 1); onAc ...
- 转: fscanf()函数详解
以前解析有规律的文件的时候要么用正则表达式,要么就是傻傻的自己写程序来解析有规律的文件.今天突然发现c的库函数中有一个现成的可以解析有规律的文件的函数,就是fscanf()函数.哎 以前自己做了这么多 ...
- iconv any encoding to UTF-8
http://stackoverflow.com/questions/9824902/iconv-any-encoding-to-utf-8
- 【转】VS2013中如何解决error C4996: 'fopen'问题
原文网址:http://jingyan.baidu.com/article/ce436649fd61543773afd32e.html 今天编写控制台应用程序时出现如下错误 error C4996: ...
- Android显示表情对话框
final EditText editface=(EditText) this.findViewById(R.id.EditText); List<Map<String,Object> ...
- Ubuntu 下配置Samba 实现Linux和windows之间文件共享
一 Samba 的安装: sudo apt-get insall sambasudo apt-get install smbfs 二 在Ubuntu中创建共享目录: mkdir /home/shar ...
- Centos6 下启动httpd报错 Could not reliably determine the server's解决方法
在启动httpd的时候报错: 修改/etc/httpd/conf/httpd.conf 配置,去掉ServerName 前的#(或者手动添加ServerName localhost:80)然后重启ht ...
- windows服务程序
首先创建一个myService的窗体程序作为服务安装卸载控制器(管理员身份运行vs,windows服务的安装卸载需要管理员权限) 在同一个解决方案里面添加一个windows服务程序,取名myWin ...
- 漏洞都是怎么编号的CVE/CAN/BUGTRAQ/CNCVE/CNVD/CNNVD
在一些文章和报道中常常提到安全漏洞CVE-1999-1046这样的CVE开头的漏洞编号,这篇文章将常见的漏洞ID的表示方法做下介绍: 1.以CVE开头,如CVE-1999-1046这样的 CVE 的英 ...
- SOA——面向服务的体系架构
上一篇博文中提到了"紧耦合"的现象.怎样解决?SOA.採用面向服务的体系架构. 一.What? SOA=Service-oriented Architecture面向服务的体系结构 ...