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 相关文章推荐
python连接sql server乱码的解决方法
Jan 28 Python
Python爬虫实战:分析《战狼2》豆瓣影评
Mar 26 Python
解决Pycharm下面出现No R interpreter defined的问题
Oct 29 Python
使用pandas实现csv/excel sheet互相转换的方法
Dec 10 Python
django settings.py 配置文件及介绍
Jul 15 Python
Django 实现admin后台显示图片缩略图的例子
Jul 28 Python
Django用户认证系统 User对象解析
Aug 02 Python
Python字符串中添加、插入特定字符的方法
Sep 10 Python
详解使用django-mama-cas快速搭建CAS服务的实现
Oct 30 Python
python实现录屏功能(亲测好用)
Mar 02 Python
Python3.9 beta2版本发布了,看看这7个新的PEP都是什么
Jun 10 Python
python/golang实现循环链表的示例代码
Sep 14 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 字符过滤类,用于过滤各类用户输入的数据
2009/05/27 PHP
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
2011/05/09 PHP
php利用curl抓取新浪微博内容示例
2014/04/27 PHP
ThinkPHP学习笔记(一)ThinkPHP部署
2014/06/22 PHP
PHP实现基于mysqli的Model基类完整实例
2016/04/08 PHP
PHP清除缓存的几种方法总结
2017/09/12 PHP
在JavaScript中获取请求的URL参数[正则]
2010/12/25 Javascript
js 程序执行与顺序实现详解
2013/05/13 Javascript
javascript垃圾收集机制与内存泄漏详细解析
2013/11/11 Javascript
JS实现滑动菜单效果代码(包括Tab,选项卡,横向等效果)
2015/09/24 Javascript
jQuery+ajax的资源回收处理机制分析
2017/01/07 Javascript
jQuery插件之validation插件
2017/03/29 jQuery
php 修改密码实现代码
2017/05/24 Javascript
javascript简写常用的12个技巧(可以大大减少你的js代码量)
2020/03/28 Javascript
使用Nodejs连接mongodb数据库的实现代码
2017/08/21 NodeJs
JavaScript对象拷贝与Object.assign用法实例分析
2018/06/20 Javascript
vue实现城市列表选择功能
2018/07/16 Javascript
JS如何把字符串转换成json
2020/02/21 Javascript
nodejs中使用worker_threads来创建新的线程的方法
2021/01/22 NodeJs
python放大图片和画方格实现算法
2018/03/30 Python
浅谈Pandas 排序之后索引的问题
2018/06/07 Python
python运行时强制刷新缓冲区的方法
2019/01/14 Python
python连接mongodb集群方法详解
2020/02/13 Python
PYcharm 激活方法(推荐)
2020/03/23 Python
Ubuntu18.04安装 PyCharm并使用 Anaconda 管理的Python环境
2020/04/08 Python
特罗佩亚包官方网站:Tropea
2017/01/03 全球购物
英国网上电器商店:Electricshop
2020/03/15 全球购物
天鹅的故事教学反思
2014/02/04 职场文书
人力资源主管的岗位职责
2014/03/15 职场文书
食品采购员岗位职责
2014/04/14 职场文书
搞笑车尾标语
2014/06/23 职场文书
毕业设计论文评语
2014/12/31 职场文书
前台接待岗位职责
2015/02/03 职场文书
小学运动会加油词
2015/07/18 职场文书
《鸡兔同笼》教学反思
2016/02/19 职场文书
Go语言带缓冲的通道实现
2021/04/26 Golang