mysql自动删除90天前数据

时间:2023-03-08 16:36:45

#coding:utf-8
import MySQLdb

#方法1直接在Navicat中添加计划任务
#DELETE FROM message2 where SEND_TIME < UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL 90 day))

#方法2
conn= MySQLdb.connect(
host='xxx.xxx.xxx.xxx',
port = 3306,
user='root',
passwd='xxxx',
db ='xxx',
)

cur = conn.cursor()
cur.execute("DELETE FROM message2 where SEND_TIME < UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL 90 day))")
cur.close()
conn.commit()
conn.close()

'''
#方法3
获取当前时间减去90天,删除90天前数据
import datetime

today = datetime.date.today()
print today

yesterday = today - datetime.timedelta(days=60)
print yesterday

cur.execute("DELETE FROM message2 WHERE SEND_TIME < UNIX_TIMESTAMP('"+ str(yesterday) + " 00:00:00')")