看完视频,自己练习一遍. 还是遇到问题,不过最终还是解决了.贴上完成的代码.

  1. CREATE TABLE `NewTable` (
  2. `acctid` int(11) NOT NULL AUTO_INCREMENT COMMENT '账户ID' ,
  3. `money` int(11) NULL DEFAULT NULL COMMENT '余额' ,
  4. PRIMARY KEY (`acctid`)
  5. )
  6. ENGINE=InnoDB
  7. DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
  8. AUTO_INCREMENT=1
  9. ROW_FORMAT=COMPACT
  10. ;

  

  1. # -*- coding:utf-8 -*-
  2. '''
  3. Created on 2015年10月6日
  4.  
  5. @author: WXG
  6. '''
  7. import MySQLdb
  8.  
  9. class TranslateAccount(object):
  10. def __init__(self, conn):
  11. self.conn = conn
  12.  
  13. def checkAccount(self, acctid):
  14. cursor = self.conn.cursor()
  15. try:
  16. sql = "select * from account where acctid = %s" % acctid
  17. print "sql for checkAccount:" + sql
  18. cursor.execute(sql)
  19. rs = cursor.fetchall()
  20. if len(rs) < 1 :
  21. raise Exception("不存在此账号%s" % acctid)
  22. finally:
  23. cursor.close()
  24.  
  25. def checkEnoughMoney(self, acctid, money):
  26. cursor = self.conn.cursor()
  27. try:
  28. sql = "select * from account where acctid = %s and money > %s" % (acctid, money)
  29. print "sql for checkEnoughMoney:" + sql
  30. cursor.execute(sql)
  31. rs = cursor.fetchall()
  32. if len(rs) < 1 :
  33. raise Exception("此账号%s没有足够的余额" % acctid)
  34. finally:
  35. cursor.close()
  36.  
  37. def reduceMoney(self, acctid, money):
  38. cursor = self.conn.cursor()
  39. try:
  40. sql = "update account set money = money-%s where acctid = %s" % (money,acctid)
  41. print "sql for reduceMoney:" + sql
  42. cursor.execute(sql)
  43. if cursor.rowcount != 1 :
  44. raise Exception("此账号%s减款失败!" % acctid)
  45. finally:
  46. cursor.close()
  47.  
  48. def addMoney(self, acctid, money):
  49. cursor = self.conn.cursor()
  50. try:
  51. sql = "update account set money = money+%s where acctid = %s" % (money,acctid)
  52. print "sql for addMoney:" + sql
  53. cursor.execute(sql)
  54. if cursor.rowcount != 1 :
  55. raise Exception("此账号%s加款失败!" % acctid)
  56. finally:
  57. cursor.close()
  58.  
  59. def translate(self, source_acctid, target_acctid, money):
  60. try:
  61. self.checkAccount(source_acctid)
  62. self.checkAccount(target_acctid)
  63. self.checkEnoughMoney(source_acctid, money)
  64. self.reduceMoney(source_acctid, money)
  65. self.addMoney(target_acctid, money)
  66. self.conn.commit()
  67. except Exception as e:
  68. self.conn.rollback()
  69. print "遇到异常%s,执行回滚!" % e
  70.  
  71. if __name__ == "__main__":
  72. source_acctid = raw_input("Source Account ID:")
  73. target_acctid = raw_input("Target Account ID:")
  74. money = raw_input("Translate Money:")
  75. conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='blog', charset='utf8')
  76. try:
  77. transAccount = TranslateAccount(conn)
  78. transAccount.translate(source_acctid, target_acctid, money)
  79. finally:
  80. conn.close()

  

python-MySQLdb-练习的更多相关文章

  1. Python MySQLdb在Linux下的快速安装

    在家里windows环境下搞了一次 见   python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...

  2. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  3. cygwin 下安装python MySQLdb

    cygwin 下安装python MySQLdb 1) cygwin 更新 运行 cygwin/setup-x86_64.exe a 输入mysql,选择下面的包安装: libmysqlclient- ...

  4. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  5. python MySQLdb在windows环境下的快速安装

    python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...

  6. windows 环境下安装python MySQLdb

    使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...

  7. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  8. 117、python MySQLdb在windows环境下的快速安装、问题解决方式

    使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...

  9. python MySQLdb Windows下安装教程及问题解决方法(python2.7)

    使用python访问mysql,需要一系列安装 linux下MySQLdb安装见  Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...

  10. macOS安装Python MySQLdb

    macOS安装Python MySQLdb 0. 参考 Mac OS X - EnvironmentError: mysql_config not found 1. 背景 import MySQLdb ...

随机推荐

  1. Step-by-Step Guide to Portal Development for Microsoft Dynamics CRM - 摘自网络

    The Challenge Oftentimes in the world of Dynamics CRM, the need arises for non-CRM users to gain acc ...

  2. 部署 外网 ASP.NET程序时, IIS安全性 配置 -摘自网络

    最近,和朋友们在聊及ASP.NET程序的安全性没有JAVA高,IIS(Internet Infomartion Server)的存在很多漏洞(以及新型蠕虫,例如Code Red 和Nimda),安全得 ...

  3. fcntl记录锁实例

    fcntl 函数是一个相当常用的对文件进行加锁操作的函数. 文件锁包括强制锁.建议锁.记录锁, 不过一般系统和内核都是用的强制锁. 以下为记录锁的例子:------------------------ ...

  4. Android模拟器如何加载本机地址及访问本机服务器

    首先获取本机ip地址: 在cmd 命令窗口中输入 ipconfig 查看本地电脑ip地址如下: 获取服务器上的Json数据,并返回结果,部分代码如下: 注:StreamUtils是自定义的一个工具类, ...

  5. SRM566 1000pts

    绍一的模拟赛题 [题意] 小Z养了$

  6. js select onchange事件

    <select id='a' name='a' onchange="javascript:alert('测试');">

  7. android106 C基本数据类型

    #JNI java native interface #c的基本数据类型 * int:32位,能表示的数字是2的32次方个 * 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是 ...

  8. List IEnumerable

    //按部门汇总            IEnumerable<WeekReportWithDepartmentInfo> report = summary            .Grou ...

  9. Cygwin下安装vim后,vim中退格键无法正常使用

    问题描述: 在Cygwin中安装完vim后 进入vim,发现上下左右键和退格键都无法正常使用 问题分析: 首先考虑到的就是缺少vim的配置文件,首先查看/etc路径下是否有vim的配置文件 admin ...

  10. ffmpeg之yuv2rgb_c_24_rgb

    YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, ) LOADCHROMA(); PUTRGB24(dst_1, py_1, ); PUTRGB24(dst_2, py_2 ...