import pymysql import datetime current_date = datetime.datetime.now().strftime('%Y-%m-%d') base_date = datetime.date.today() - datetime.timedelta(days=1) def clear_info(base_date, table='human_dialogue_Jan'): connection = pymysql.connect(host="47.92.193.147", port=3306, user="root", passwd="Moxi123#", db="task_dialogue_config", charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() sql = '' if 'nlu_result' in table: sql = """delete from {} where date_format(create_time, '%Y-%m-%d') < '{}' """.format(table, base_date) elif 'human_dialogue' in table: sql = """delete from {} where date_format(call_start_time, '%Y-%m-%d') < '{}' """.format(table, base_date) print(sql) if sql: cursor.execute(sql) connection.commit() connection.close() if __name__ == '__main__': clear_info(base_date, 'human_dialogue_Jan') clear_info(base_date, 'human_dialogue_nlu_result_Jan')