之前编写了对mysql数据库的增,删,改,查的方法,再见这部分的代码:
import MySQLdb
class MySQLHelper(object):
def __init__(self):
pass
@property
def selectMySQL(self):
try:
conn=MySQLdb.connect(host='localhost',user='root',
passwd='server',db='db',,charset='utf8')
cur=conn.cursor()
except Exception,e:
print u'操作mysql数据库失败'
else:
cur.execute('select * from user;')
data=cur.fetchall()
for d in data:
print d
finally:
cur.close()
conn.close()
@property
def insertMySQL(self):
try:
conn=MySQLdb.connect(host='localhost',user='root',
passwd='server',db='db',,charset='utf8')
cur=conn.cursor()
sql="insert into user(id,name,sex,email) VALUES (%s,%s,%s,%s)"
params=(1,'selenium2','boy','USA@qq.com')
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'插入后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
@property
def updateMySQL(self):
try:
conn=MySQLdb.connect(host='localhost',user='root',
passwd='server',db='db',,charset='utf8')
cur=conn.cursor()
sql="update user set NAME=%s where id=%s"
params=('selenium webdriver',1)
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'修改后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
@property
def deleteMySQL(self):
try:
conn=MySQLdb.connect(host='localhost',user='root',
passwd='server',db='db',,charset='utf8')
cur=conn.cursor()
sql="delete from USER WHERE id=%s"
params=(1)
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'删除后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
发现在增,删,改,查方法中,都有MySQLdb.connect(host='localhost',user='root',passwd='server',db='db',,charset='utf8'),这意味着什么?意味着对mysql操作的方法,都得写MySQLdb.connect(host='localhost',user='root',passwd='server',db='db',,charset='utf8'),有多少个方法,就得写多少个,这样明显是不合理的,很明显是重复造轮子,而且也不符合python的简单而优雅的设计理念,那么开始重构这部分的代码,把一个配置文件config.py或者写一个类Config,把连接mysql的部分写成一个静态方法,有操作mysql的方法,直接调用即可。这里创建config.py的模块,见config.py模块的源码:
#coding:utf-8
conn=dict(host='127.0.0.1',user='root',passwd='server',db='db',charset='utf8')
见重构后的MySQLHelper类的源码:
#coding:utf-8
import MySQLdb
import config
class MySQLHelper(object):
def __init__(self):
self.__conn=config.conn
@property
def selectMySQL(self):
try:
conn=MySQLdb.connect(**self.__conn)
cur=conn.cursor()
except Exception,e:
print u'操作mysql数据库失败'
else:
cur.execute('select * from user;')
data=cur.fetchall()
for d in data:
print d
finally:
cur.close()
conn.close()
@property
def insertMySQL(self):
try:
conn=MySQLdb.connect(**self.__conn)
cur=conn.cursor()
sql="insert into user(id,name,sex,email) VALUES (%s,%s,%s,%s)"
params=(1,'selenium2','boy','USA@qq.com')
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'插入后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
@property
def updateMySQL(self):
try:
conn=MySQLdb.connect(**self.__conn)
cur=conn.cursor()
sql="update user set NAME=%s where id=%s"
params=('selenium webdriver',1)
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'修改后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
@property
def deleteMySQL(self):
try:
conn=MySQLdb.connect(**self.__conn)
cur=conn.cursor()
sql="delete from USER WHERE id=%s"
params=(1)
cur.execute(sql,params)
conn.commit()
except Exception,e:
print u'操作mysql数据库失败'
else:
print u'删除后表的数据为:'
self.selectMySQL
finally:
cur.close()
conn.close()
注解:把连接mysql的部分写在了config.py模块中,在该模块中,conn=dict(host='127.0.0.1',user='root',passwd='server',db='db',charset='utf8'),conn是一个字典,在类MySQKHelper中,初始化self.__conn=config.conn为私有字段,直接调用,这样连接mysql 的方法就直接即可。