[python]代码库
import schedule
import codecs
import csv
import time
from sqlalchemy import create_engine
def get_conn():
engine = create_engine("mysql+pymysql://root:password@localhost:3306/test?charset=utf8mb4")
conn = engine.connect()
return conn
def query():
sql = "select * from user limit 10"
conn = get_conn()
return conn.execute(sql)
def read_mysql_to_csv(filename):
with codecs.open(filename=filename, mode='w') as f:
write = csv.writer(f, dialect='excel')
results = query()
for result in results:
write.writerow(result)
schedule.every().day.at("03:00").do(read_mysql_to_csv, "test")
while True:
schedule.run_pending()
time.sleep(10)