前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python对mysql数据库操作之代码优化(二)

python对mysql数据库操作之代码优化(二)

作者头像
无涯WuYa
发布2018-10-25 16:11:14
9530
发布2018-10-25 16:11:14
举报

之前编写了对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 的方法就直接即可。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-12-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档