我有写到mysql数据库的python3程序,并且如果自上次输入以来已超过15分钟,我希望python3程序发送警报。问题是我不知道如何从mysql datetime表条目中减去python3 datetime.now()
谢谢
我有写到mysql数据库的python3程序,并且如果自上次输入以来已超过15分钟,我希望python3程序发送警报。问题是我不知道如何从mysql datetime表条目中减去python3 datetime.now()
谢谢
我不太清楚表中存储的日期时间,如果您以正确的格式存储日期时间,则只需进行常规减法即可。
import datetime
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",user="dummyuser",# Enter your username
passwd="dummypass",# Enter your password
database="mydatebase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT date_time_column FROM mytable ORDER BY id DESC LIMIT 1;")
curr_date = datetime.datetime.now()
print("Current datetime Type:",type(curr_date))
print("Current datetime:",curr_date)
for x in mycursor:
y = x[0]
print("DB datetime Type",type(y))
print("DB datetime",y)
print("Difference between current time and DB time:",curr_date - y)
输出:
Current datetime Type: <class 'datetime.datetime'>
Current datetime: 2019-11-13 15:28:03.908454
DB datetime Type <class 'datetime.datetime'>
DB datetime 2019-10-27 15:07:04.378052
Difference between current time and DB time: 17 days,0:20:59.530402