需要连接数据库的场景,比如注册界面,同一个账号不能重复注册,比如流程界面:登录-绑定银行卡-解绑银行卡在测试和解绑银行卡的时候,需要创建数据修改先绑定银行卡状态接口,比如一个接口有几个状态:绑定-使用-注销测试接口不同状态时,需要恢复数据和清理数据.测试完成后清理垃圾数据安装pipinstallPyMySQL连接数据库importpymysql#数据库信息db_info={"host":"127.0.0.1",#ip"user":"root",#username"password":"123456",#password"port":3306}#portclassDbConnect():def__init__(self,db_cof,database=""):self.db_cof=db_cof#打开数据库连接self.db=pymysql.connect(database=database,cursorclass=pymysql.cursors.DictCursor,**db_cof)#使用cursor()方法获取操作游标self.cursor=self.db.cursor()defselect(self,sql):#SQL查询语句#sql="SELECT*FROMEMPLOYEE\#WHEREINCOME>%s"%(1000)self.cursor.execute(sql)results=self.cursor.fetchall()returnresultsdefexecute(self,sql):#SQL删除、提交、修改语句#sql="DELETEFROMEMPLOYEEWHEREAGE>%s"%(20)try:#执行SQL语句self.cursor.execute(sql)#提交修改self.db.commit()except:#出错时回滚self.db.rollback()发生defclose(self):#关闭连接self.db.close()defselect_sql(select_sql):'''querydatabase'''db=DbConnect(dbinfo,database="apps")result=db.select(select_sql)#查询db.close()returnresultdefexecute_sql(insert_sql):'''Executesql'''db=DbConnect(dbinfo,database="apps")db.execute(insert_sql)#查询db.close()
