介绍:本文是关于数据库的简单操作,实现转账(只是修改数据库中用户的账户金额)的功能

模块介绍:首先是入口主函数 主函数中实现转账方法  以及异常的处理:

if __name__ == "__main__":
source_acctid = sys.argv[1] #获取命令行第一个参数
target_acctid = sys.argv[2]
money = sys.argv[3] #连接数据库
conn = MySQLdb.Connect(host = '127.0.0.1',user = 'root',passwd = 'root',port = 3306,db = 'account')
tr_money = TransferMoney(conn) try:
tr_money.transfer(source_acctid,target_acctid,money) #转账操作
except Exception as e:
print "出现问题" + str(e)
finally:
conn.close()#关闭数据库连接

然后是实现转账的类,以及类中实现的方法 1.判断用户账户中的金额 2.判断账户是否存在 3.扣除账户金额  4.增加账户金额

class TransferMoney(object):

    def __init__(self, conn):
self.conn = conn def check_acct_available(self, acctid):
cursor = self.conn.cursor()
try:
sql = "select * from account_user where accountid=%s"%acctid
cursor.execute(sql)
print "check_acct_available:"+sql
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception("账号%s不存在"%acctid)
finally:
cursor.close() def has_enough_money(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "select * from account_user where accountid=%s and money>%s"% (acctid,money)
cursor.execute(sql)
print "has_enough_money:"+sql
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception("账号%s没有足够的钱"%acctid)
finally:
cursor.close() def reduce_money(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "update account_user set money=money-%s where accountid=%s"% (money,acctid)
cursor.execute(sql)
print "reduce_money:"+sql
if cursor.rowcount!=1:
raise Exception("账号%s减款失败"%acctid)
finally:
cursor.close() def add_money(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "update account_user set money=money+%s where accountid=%s"% (money,acctid)
cursor.execute(sql)
print "add_money:"+sql
if cursor.rowcount!=1:
raise Exception("账号%s加款失败"%acctid)
finally:
cursor.close() def transfer(self,source_acctid,target_acctid,money):
try:
self.check_acct_available(source_acctid) #检查转账账户是否可用
self.check_acct_available(target_acctid)
self.has_enough_money(source_acctid,money) #检查账户钱是否足够
self.reduce_money(source_acctid,money) #扣除钱
self.add_money(target_acctid,money) #转账加钱
self.conn.commit()
except Exception as e:
self.conn.rollback() #如果有步骤出错 则事务回滚
raise e # raise的作用是将异常抛出到上一级处理

python 简单的数据库操作之转账的更多相关文章

  1. [python]用Python进行SQLite数据库操作

    用Python进行SQLite数据库操作 1.导入Python SQLITE数据库模块 Python2.5之后,内置了SQLite3,成为了内置模块,这给我们省了安装的功夫,只需导入即可~  ]: u ...

  2. 使用python简单连接并操作数据库

    python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...

  3. python对mysql数据库操作的三种不同方式

    首先要说一下,在这个暑期如果没有什么特殊情况,我打算用python尝试写一个考试系统,希望能在下学期的python课程实际使用,并且尽量在此之前把用到的相关技术都以分篇博客的方式分享出来,有想要交流的 ...

  4. Python进行MySQL数据库操作

    最近开始玩Python,慢慢开始喜欢上它了,以前都是用shell来实现一些自动化或者监控的操作,现在用Python来实现,感觉更棒,Python是一门很强大的面向对象语言,所以作为一个运维DBA或者运 ...

  5. PHP开发-最简单的数据库操作,使用ezSQL

    PHP数据库操作使用ezSQL来实现,简单好用. 如果用的是mysql数据库,将下载的ezSQL文件中的mysql和shared连个文件夹拷贝到PHP工程目录中引用即可. 在PHP文件中 // Inc ...

  6. python之(mysql数据库操作)

    前言:关心3步骤(此文章只针对python自动化根基展开描述) 什么是mysql数据库操作?  答:利用python对mysql数据库进行增, 删, 改, 查 操作 为什么要用python对mysql ...

  7. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  8. [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  9. Django简单的数据库操作

    当然,本篇的前提是你已经配置好了相关的环境,这里就不详细介绍. 一. 在settings.py文件中设置数据库属性. 如下: DATABASES = { 'default': { 'ENGINE': ...

随机推荐

  1. LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. APP登录的机制

    1.APP每次发送请求时,都会发送header给服务器,服务器去校验传过来的信息是否正确:校验成功后登录成功,若传入的信息不符合该用户的信息则服务器判断,传给APP登录失败 每次的请求都会传入上图中的 ...

  3. CodeForces - 794C:Naming Company(博弈&简单贪心)

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  4. vertex shader must minimally write all four components of POSITION

    Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...

  5. Qt Creator 中自动补全的快捷键描述

    在TextEdit中的completeThis

  6. 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT

    题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...

  7. Kosaraju

    https://www.cnblogs.com/nullzx/p/6437926.html

  8. Hybrid APP混合开发

    写在前面: 由于业务需要,接触到一个Hybrid APP混合开发的项目.当时是第一次接触混合开发,有一些经验和总结,欢迎各位一起交流学习~ 1.混合开发概述 Hybrid App主要以JS+Nativ ...

  9. [转]ubuntu11.04配置nfs--解决mount.nfs: access denied问题

    总算通过了nfs的localhost测试. 配置很简单,下面摘自网络,并且整理下: 1 安装nfs #apt-get install nfs-kernel-server #apt-get instal ...

  10. __thiscalll C++底层识别成员函数

    问题描述: class myClass { public: void SetNumber(int nNumber) { m_nInt = nNumber; } private: int m_nInt; ...