defsave_as_excel(self):# 打开数据库连接
db = pymysql.connect("10.150.154.190","reporter","Reporter01","reports", charset='utf8')# 使用cursor()方法获取操作游标
cursor = db.cursor()
date =input("请输入开始时间:格式(年-月-日 00:00:00)")# date = '2020-12-20 10:00:00'# 筛选
sql = f'''SELECT versionPath,pass_rate,detail,end_time,analysis
FROM tb_report_smoke_brief as a
INNER JOIN tb_test_event as b
WHERE a.test_event_id = b.id
AND end_time > '{date}'
AND (pass_rate NOT LIKE '100%' OR detail not LIKE 'ok')'''try:print("执行查询语句")
cursor.execute(sql)#执行命令
db.commit()# 关闭数据库
cursor.close()
db.close()
myresult = cursor.fetchall()# 打开excelprint("查询结果写入excel")
wb = openpyxl.load_workbook(path)
ws = wb.active
i =1for x in myresult:print(x)for j inrange(0,len(x)):
ws.cell(row=i, column=j+1).value = x[j]
i +=1
wb.save(path)except:print("error")