gpt4 book ai didi

mysql - python3计算自上次mysql数据输入以来的分钟数

转载 作者:行者123 更新时间:2023-11-29 15:29:30 28 4
gpt4 key购买 nike

我有一个 python3 程序,可以写入 mysql 数据库,并且我希望 python3 程序在自上次输入以来已经超过 15 分钟时发送警报。问题是我不知道如何从 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)

输出:

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

关于mysql - python3计算自上次mysql数据输入以来的分钟数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830382/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com