python-MySQLdb-练习
看完视频,自己练习一遍. 还是遇到问题,不过最终还是解决了.贴上完成的代码.
CREATE TABLE `NewTable` (
`acctid` int(11) NOT NULL AUTO_INCREMENT COMMENT '账户ID' ,
`money` int(11) NULL DEFAULT NULL COMMENT '余额' ,
PRIMARY KEY (`acctid`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1
ROW_FORMAT=COMPACT
;
# -*- coding:utf-8 -*-
'''
Created on 2015年10月6日 @author: WXG
'''
import MySQLdb class TranslateAccount(object):
def __init__(self, conn):
self.conn = conn def checkAccount(self, acctid):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid = %s" % acctid
print "sql for checkAccount:" + sql
cursor.execute(sql)
rs = cursor.fetchall()
if len(rs) < 1 :
raise Exception("不存在此账号%s" % acctid)
finally:
cursor.close() def checkEnoughMoney(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid = %s and money > %s" % (acctid, money)
print "sql for checkEnoughMoney:" + sql
cursor.execute(sql)
rs = cursor.fetchall()
if len(rs) < 1 :
raise Exception("此账号%s没有足够的余额" % acctid)
finally:
cursor.close() def reduceMoney(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "update account set money = money-%s where acctid = %s" % (money,acctid)
print "sql for reduceMoney:" + sql
cursor.execute(sql)
if cursor.rowcount != 1 :
raise Exception("此账号%s减款失败!" % acctid)
finally:
cursor.close() def addMoney(self, acctid, money):
cursor = self.conn.cursor()
try:
sql = "update account set money = money+%s where acctid = %s" % (money,acctid)
print "sql for addMoney:" + sql
cursor.execute(sql)
if cursor.rowcount != 1 :
raise Exception("此账号%s加款失败!" % acctid)
finally:
cursor.close() def translate(self, source_acctid, target_acctid, money):
try:
self.checkAccount(source_acctid)
self.checkAccount(target_acctid)
self.checkEnoughMoney(source_acctid, money)
self.reduceMoney(source_acctid, money)
self.addMoney(target_acctid, money)
self.conn.commit()
except Exception as e:
self.conn.rollback()
print "遇到异常%s,执行回滚!" % e if __name__ == "__main__":
source_acctid = raw_input("Source Account ID:")
target_acctid = raw_input("Target Account ID:")
money = raw_input("Translate Money:")
conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='blog', charset='utf8')
try:
transAccount = TranslateAccount(conn)
transAccount.translate(source_acctid, target_acctid, money)
finally:
conn.close()
python-MySQLdb-练习的更多相关文章
- Python MySQLdb在Linux下的快速安装
在家里windows环境下搞了一次 见 python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...
- #MySQL for Python(MySQLdb) Note
#MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...
- cygwin 下安装python MySQLdb
cygwin 下安装python MySQLdb 1) cygwin 更新 运行 cygwin/setup-x86_64.exe a 输入mysql,选择下面的包安装: libmysqlclient- ...
- Python MySQLdb模块连接操作mysql数据库实例_python
mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...
- python MySQLdb在windows环境下的快速安装
python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...
- windows 环境下安装python MySQLdb
使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...
- python MySQLdb连接mysql失败(转载)
最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...
- 117、python MySQLdb在windows环境下的快速安装、问题解决方式
使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...
- python MySQLdb Windows下安装教程及问题解决方法(python2.7)
使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...
- macOS安装Python MySQLdb
macOS安装Python MySQLdb 0. 参考 Mac OS X - EnvironmentError: mysql_config not found 1. 背景 import MySQLdb ...
随机推荐
- linux 安装java环境(jdk)
第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...
- centos6 kvm网卡桥接
以前用VMware,我的上司说,你既然都用CentOS的桌面,那就研究一下KVM. 好吧,上司做运维好几年了,就听了他的,装了一个KVM. KVM的网络默认是NAT,不方便,就学习BRIDGE!!! ...
- Yii框架CGridView columns中使用数组或变量传值
继续Yii框架的学习与使用,CGridView对于网站后台而言十分有用处,可以很快速地实现数据列表显示,并集成排序,搜索等功能,很巧妙. 今天,在项目中遇到了状态显示问题,在controller中定义 ...
- String的点点滴滴
一.String 的 equals()到底比较的是什么?equals() 与 == 的区别? 当使用关系运算符==比较两个对象时,是比较两个对象使用的内存地址和内容是否相同,如果两个对象使用的是同一个 ...
- C# DataTable的詳細使用方法
在项目中经经常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTabl ...
- SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' 的访问
sqlserver2008导入excel到数据库的时候报错: SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' ...
- [React] Creating a Stateless Functional Component
Most of the components that you write will be stateless, meaning that they take in props and return ...
- ERROR 1045: Access denied for user: 'root@localhost' (Using password: YES)(转)
前两天也偶尔出现这个错误,也没在意,因为我重新修改一下mysql的root密码后又可以用了,但昨天却不行,我把root密码修改以后虽然当时能用, 一旦重新进入就都不能用了,可我的密码明明没有错啊?今天 ...
- Java 向Hbase表插入数据报(org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apac)
org.apache.hadoop.hbase.client.HTablePool$PooledHTable cannot be cast to org.apac 代码: //1.create HTa ...
- android103 内容观察者
#内容观察者 * 通过内容提供者可以访问到数据库,当数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知,类似于广播接受者,但是他不是广播. cr ...