import datetime import pymysql import pymysql.cursors tables_schdule=["talbe1","table2","table3","table4","table5"] x = datetime.date.today().replace(day=1) y = datetime.timedelta(days=1) last_month = (x-y).strftime("%Y%m") #print(last_month) drop_tables=[] for i in range(5): drop_tables.append("ALTER TABLE {} TRUNCATE PARTITION P{};".format(tables_schdule[i],last_month)) #print(drop_tables) connection=pymysql.connect('127.0.0.1','root','duzhaoqi2015','eos') try: #获取一个游标 with connection.cursor() as cursor: # sql='select * from test' # cursor.execute(sql) for i in drop_tables: #print(i) cursor.execute(i) # for row in cursor.fetchall(): # print("ID:" + str(row[0]) + ' 名字:' + row[1]) # connection.commit() finally: connection.close()