Python操作MySQL模拟银行转账

yipeiwu_com6年前Python基础

今天在慕课网上学习了有关于python操作MySQL的相关知识,在此做些总结。python操作数据库还是相对比较简单的,由于python统一了各个数据库的接口程序,也就是所谓的Python DB,所以无论使用何种数据可,都可以用统一的接口对数据库进行操作。操作中主要涉及connection对象的操作和cursor的操作,前者主要是为了建立起python与数据库的数据交换通道,后者则是访问数据的游标,也可以理解为指针。数据库的相关结构化语言在Python中均是以字符串的形式呈现的。另外注意rollback的重要性,一旦操作失败,所有操作都要回滚到之前的状态,否则会发生错误。

另外,在编写实例的时候,对于面向对象的编程思路又有了新的认识,自顶向下的程序编写模式非常有利于拆分程序的功能,分而治之。面向对象的封装性在此提醒的淋漓尽致!

代码如下,在原有基础上,我又增加了添加记录的功能。

#coding=utf8 
import MySQLdb 
import sys 
 
class TranseferMonet(object): 
 
  def __init__(self,conn): 
    self.conn = conn 
 
  def createNewUser(self,userID,money): 
    cursor = self.conn.cursor() 
    try: 
      sql = 'INSERT account VALUES(%s,%s)' %(str(userID),str(money)) 
      cursor.execute(sql) 
      self.conn.commit() 
    except Exception as e: 
      self.conn.rollback() 
      raise e 
 
  def transferMoney(self,transeferID,recivierID,money): 
    try: 
      self.checkID(transeferID) 
      self.checkID(receiverID) 
      self.checkEnoughMoney(transferID,money) 
      self.subMoney(transferID,money) 
      self.addMoney(receiverID,money) 
      self.conn.commit() 
    except Exception as e: 
      self.conn.rollback() 
      raise e 
 
  def checkID(self,userID): 
    cursor = self.conn.cursor() 
    try: 
      sql = 'SELECT userID FROM account WHERE userID = %s' %str(userID) 
      cursor.execute(sql) 
      rs = cursor.fetchall() 
      if len(rs) != 1: 
        raise Exception("ID错误!") 
    finally: 
      cursor.close() 
 
  def checkEnoughMoney(self,transferID,money): 
    cursor = self.conn.cursor() 
    try: 
      sql = 'SELECT money FROM account WHERE userID = %s and money >= %s' %(str(transferID),str(money)) 
      cursor.execute(sql) 
      rs = cursor.fetchall() 
      if len(rs) != 1: 
        raise Exception("余额不足!") 
    finally: 
      cursor.close() 
  def subMoney(self,transferID,money): 
    cursor = self.conn.cursor() 
    try: 
      sql = 'UPDATE account SET money = money-%s WHERE userID = %s' %(str(money),str(transferID)) 
      cursor.execute(sql) 
      if cursor.rowcount != 1: 
        raise Exception('减款失败!') 
    finally: 
      cursor.close() 
 
  def addMoney(self,receiverID,money): 
 
    cursor = self.conn.cursor() 
    try: 
      sql = 'UPDATE account SET money = money+%s WHERE userID = %s' %(str(money),str(receiverID)) 
      cursor.execute(sql) 
      if cursor.rowcount != 1: 
        raise Exception('加款失败!') 
    finally: 
      cursor.close() 
 
if __name__=="__main__": 
 
  transferID = 2002 
  receiverID = 2001 
  money = 300 
 
  newID = 2003 
  newmoney = 900 
 
  conn = MySQLdb.connect(host = '127.0.0.1',port = 3306,user = 'root',passwd = '914767195',db = 'test',charset = 'utf8') 
 
  trMoney = TranseferMonet(conn) 
 
  try: 
    trMoney.transferMoney(transferID,receiverID,money) 
  except Exception as e: 
    print "转账错误"+str(e) 
  try: 
    trMoney.createNewUser(newID,newmoney) 
  except Exception as e: 
    print "创建用户失败!"+str(e) 
  finally: 
    conn.close() 

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现简单生成验证码功能【基于random模块】

本文实例讲述了Python实现简单生成验证码功能。分享给大家供大家参考,具体如下: 验证码一般用来验证登陆、交易等行为,减少对端为机器操作的概率,python中可以使用random模块,...

Python面向对象程序设计类的多态用法详解

本文实例讲述了Python面向对象程序设计类的多态用法。分享给大家供大家参考,具体如下: 多态 1、多态使用 一种事物的多种体现形式,举例:动物有很多种 注意: 继承是多态的前提 函数重...

Python实现霍夫圆和椭圆变换代码详解

Python实现霍夫圆和椭圆变换代码详解

在极坐标中,圆的表示方式为: x=x0+rcosθ y=y0+rsinθ 圆心为(x0,y0),r为半径,θ为旋转度数,值范围为0-359 如果给定圆心点和半径,则其它点是否在圆上,我们...

python发送告警邮件脚本

python发送告警邮件脚本

python脚本为敏捷开发脚本,在zabbix监控也起到重要作用,以下是使用python脚本发送告警邮件配置方法。 脚本如下: #!/usr/bin/python #coding:u...

Python实现数值积分方式

Python实现数值积分方式

原理: 利用复化梯形公式,复化Simpson公式,计算积分。 步骤: import math """测试函数""" def f(x,i): if i == 1: re...