python 接口自动化测试--代码实现(七) 转
上一篇 /
下一篇 2016-10-20 13:52:44
/ 个人分类:python
数据库还原脚本:
#! /usr/bin/python#coding:utf-8importtimefromEngine.SqlEngineimportMSSQL
COUNT=1defrestoreRelease():globalCOUNT
checkConSql="select spid from sysprocesses where dbid in (select dbid from sysdatabases where name='SOATest')"restoreSql="RESTORE DATABASE SOATest FROM DATABASE_SNAPSHOT = 'SOATest_ss'"dbc=MSSQL('192.168.1.2','yht','yht','Master')
conNum=dbc.ExecQuery(checkConSql)ifCOUNT < 5:iflen(conNum) ==0:print(u'%d条连接数,可以还原数据库,还原中...'%len(conNum))
dbc.ExecNonQuery(restoreSql)print(u'数据库还原完成')returnTrueelse:print(u'%d条连接数占用无法还原数据库,5秒后重试'%len(conNum))
time.sleep(5)
COUNT=COUNT+1restoreRelease()else:print(u'%d条连接数始终占用,已试过5次依然无法还原数据库'%len(conNum))returnFalse
SqlEngine.py
#! /usr/bin/python#coding:utf-8importpymssqlimportsysfromEngine.DataEngineimportdecoCatchExcep
reload(sys)
sys.setdefaultencoding('utf-8')classMSSQL(object):"""对pymssql的简单封装
pymssql库,该库到这里下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
使用该库时,需要在Sql Server Configuration Manager里面将TCP/IP协议开启
用法:"""def__init__(self,host,user,pwd,db):
self.host=host
self.user=user
self.pwd=pwd
self.db=dbdef__GetConnect(self):"""得到连接信息
返回: conn.cursor()"""ifnotself.db:raise(NameError,"没有设置数据库信息")
self.conn= pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
cur=self.conn.cursor()ifnotcur:raise(NameError,"连接数据库失败")else:returncur
@decoCatchExcepdefExecQuery(self,sql):
cur= self.__GetConnect()
cur.execute(sql)
resList=cur.fetchall()
self.conn.close()returnresList
@decoCatchExcepdefExecNonQuery(self,sql):
cur= self.__GetConnect()
self.conn.autocommit(True)
cur.execute(sql)
self.conn.autocommit(False)
self.conn.commit()
self.conn.close()
装饰器decoCatchExcep:
defdecoCatchExcep(func):def_decoCatchExcep(*args, **kwargs):try:
ret= func(*args, **kwargs)returnretexceptException,e:print(func.__name__+':'+str(e).encode('gb18030'))
LogPro.writeException(str(e).encode('gb18030'))return_decoCatchExcep
收藏
举报
TAG:
Python
python
接口