#!/usr/bin/python
# -*- coding: UTF-8 -*-
import mysql.connector
import pandas as pd
import numpy as np
import time
import datetime
from sqlalchemy import create_engine
from apscheduler.schedulers.blocking import BlockingScheduler
def deviceInfoToExcel():
#定义变量
sql = " "
excelFile = 'F:\\tools\\python\\Excel_'+time.strftime('%Y%m%d',time.localtime())+'.xlsx'
print("开始连接数据库")
proConn = mysql.connector.connect(
host="", # 数据库主机地址
user="", # 数据库用户名
passwd="", # 数据库密码
database=""
)
print("连接数据库成功")
cursor = proConn.cursor()
cursor.execute(sql)
data = cursor.fetchall() # fetchall() 获取所有记录
columnDes = cursor.description #获取连接对象的描述信息
columnNames = [columnDes[i][0] for i in range(len(columnDes))]
df = pd.DataFrame([list(i) for i in data],columns=columnNames)
writer = pd.ExcelWriter(excelFile)
df.to_excel(writer,float_format='%.5f')
writer.save()
print("导出Excel完成")
cursor.close()
proConn.close()
print('关闭数据库连接')
def taskToExcel():
print(time.strftime('>>>>>>> now %Y-%m-%d %X',time.localtime()))
deviceInfoToExcel()
print(time.strftime('>>>>>>> now %Y-%m-%d %X',time.localtime()))
print()
if __name__ == '__main__':
# BlockingScheduler 在进程中运行单个任务 调度器是唯一运行的东西
scheduler = BlockingScheduler()
# 在 之间, 每隔60分钟执行一次 taskToExcel 方法
scheduler .add_job(taskToExcel, 'interval', minutes=60, coalesce=True, misfire_grace_time=3600,start_date='2020-06-16 19:59:00' , end_date='2020-12-31 23:59:59')
scheduler.start()