[英]python3 calculate minutes since last mysql data entry
我有寫入 mysql 數據庫的 python3 程序,如果自上次輸入超過 15 分鍾,我希望 python3 程序發送警報。 問題是我不知道如何從 mysql 日期時間表條目中減去 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)
Output:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.