Python操作MySQL模拟银行转账


Posted in Python onMarch 12, 2018

今天在慕课网上学习了有关于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 相关文章推荐
Python3实现连接SQLite数据库的方法
Aug 23 Python
django rest framework之请求与响应(详解)
Nov 06 Python
python+matplotlib绘制3D条形图实例代码
Jan 17 Python
Python多进程入门、分布式进程数据共享实例详解
Jun 03 Python
django中使用POST方法获取POST数据
Aug 20 Python
springboot配置文件抽离 git管理统 配置中心详解
Sep 02 Python
python 实现简单的FTP程序
Dec 27 Python
python图形开发GUI库wxpython使用方法详解
Feb 14 Python
python实现从ftp服务器下载文件
Mar 03 Python
Keras: model实现固定部分layer,训练部分layer操作
Jun 28 Python
python 用opencv实现图像修复和图像金字塔
Nov 27 Python
使用Python webdriver图书馆抢座自动预约的正确方法
Mar 04 Python
python3 图片referer防盗链的实现方法
Mar 12 #Python
tensorflow构建BP神经网络的方法
Mar 12 #Python
Python管理Windows服务小脚本
Mar 12 #Python
python实现教务管理系统
Mar 12 #Python
python编写弹球游戏的实现代码
Mar 12 #Python
python学生管理系统代码实现
Apr 05 #Python
python图书管理系统
Apr 05 #Python
You might like
PHP SEO优化之URL优化方法
2011/04/21 PHP
PHP7.0安装笔记整理
2015/08/28 PHP
阿里云PHP SMS短信服务验证码发送方法
2017/07/11 PHP
PHP长连接实现与使用方法详解
2018/02/11 PHP
PHP内置函数生成随机数实例
2019/01/18 PHP
Google Map V3 绑定气泡窗口(infowindow)Dom事件实现代码
2013/04/26 Javascript
Json和Jsonp理论实例代码详解
2013/11/15 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
node.js使用nodemailer发送邮件实例
2014/03/10 Javascript
jQuery移动页面开发中的触摸事件与虚拟鼠标事件简介
2015/12/03 Javascript
jQuery实现的省市县三级联动菜单效果完整实例
2016/08/01 Javascript
详解ES6 Promise对象then方法链式调用
2018/10/20 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
JS实现网站楼层导航效果代码实例
2020/06/16 Javascript
[01:09:23]KG vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
Python随机生成一个6位的验证码代码分享
2015/03/24 Python
利用Python的Flask框架来构建一个简单的数字商品支付解决方案
2015/03/31 Python
Python中tell()方法的使用详解
2015/05/24 Python
Python实现TCP/IP协议下的端口转发及重定向示例
2016/06/14 Python
django静态文件加载的方法
2018/05/20 Python
使用numpy和PIL进行简单的图像处理方法
2018/07/02 Python
Python图像处理库PIL中图像格式转换的实现
2020/02/26 Python
python 日志模块 日志等级设置失效的解决方案
2020/05/26 Python
python属于解释型语言么
2020/06/15 Python
利用Python如何画一颗心、小人发射爱心
2021/02/21 Python
德国拖鞋网站:German Slippers
2019/11/08 全球购物
学习心得体会
2014/01/01 职场文书
大学生职业生涯规划书范文
2014/01/04 职场文书
幼儿园庆六一活动方案
2014/03/06 职场文书
植物生产学专业求职信
2014/08/08 职场文书
大学生创业计划书怎么写
2014/09/15 职场文书
个人查摆问题自查报告
2014/10/16 职场文书
先进教师个人事迹材料
2014/12/15 职场文书
2016春节家属慰问信
2015/03/25 职场文书
十大动画制作软件,Adobe产品上榜两款,第一是行业标准软件
2022/03/18 杂记