试图让python sqlite与日期而不是实际日期时间字段进行比较
我有一些代码不起作用试图让python sqlite与日期而不是实际日期时间字段进行比较,sql,python-3.x,sqlite,datetime,where-clause,Sql,Python 3.x,Sqlite,Datetime,Where Clause,我有一些代码不起作用 currentTime = dt.utcnow().date() get just the day has to be UTC cause that what stored in db. print(currentTime) #shows day of 2020-09-03 cur = conn.cursor() cur.execute("SELECT f.user_id, u.first_name, f.location, f.checkin, f.check
currentTime = dt.utcnow().date() get just the day has to be UTC cause that what stored in db.
print(currentTime) #shows day of 2020-09-03
cur = conn.cursor()
cur.execute("SELECT f.user_id, u.first_name, f.location, f.checkin, f.checkout FROM auth_user u JOIN covidlog_onsitelog as f ON (f.user_id=u.id) WHERE CAST(f.checkin as 'DATE()') = ? or CAST(f.checkout as 'DATE()') =?", (currentTime,currentTime))
该字段是在django中作为模型创建的,其中包含以下项:
2020-09-03 00:40:58.901602
但我只想比较一下这一天,不管时间是什么……尽管它似乎不起作用
所以对我来说,这看起来是正确的,但并没有产生结果,所以我只能假设它仍然在某种程度上比较日期和时间戳。。。你知道如何不这样做并与日期进行比较吗?你想要date()
虽然键入的时间稍长,但如果不在表列上使用日期函数,则可以更有效地表示:
where
(f.checkin >= date(?) and f.checkin < date(?, '+1 day')
or (f.checkout >= date(?) and f.checkout < date(?, '+1 day')
在哪里
(f.签入>=日期(?)和f.签入<日期(?,+1天)
或(f.结帐>=日期(?)和f.结帐<日期(?,+1天)
是否要日期()
虽然键入的时间稍长,但如果不在表列上使用日期函数,则可以更有效地表示:
where
(f.checkin >= date(?) and f.checkin < date(?, '+1 day')
or (f.checkout >= date(?) and f.checkout < date(?, '+1 day')
在哪里
(f.签入>=日期(?)和f.签入<日期(?,+1天)
或(f.结帐>=日期(?)和f.结帐<日期(?,+1天)
检查f.checkin和f checkout字段是否为datetimestring,在这种情况下,您可以使用date(f.checkin)和date(f.checkout)代替CAST(f.checkin为'date()')
您可能想看看“https://stackoverflow.com/questions/4428795/sqlite-convert-string-to-date“以及:https://www.tutlane.com/tutorial/sqlite/sqlite-date-function#:~:text=语法%20(属于%20SQLite%20date(),日期时间%20)将%20value%20字符串转换为%20date。&text=语法%20SQLite%20date()%20函数,YYYY%2DMM%2DDD%20格式。“
您的WHERE子句如下所示:
WHERE
DATE(f.checkin) = ? OR DATE(f.checkout) = ?
检查f.checkin和f checkout字段是否为datetimestring,在这种情况下,可以使用date(f.checkin)和date(f.checkout)而不是强制转换(f.checkin为'date()') 您可能想看看“https://stackoverflow.com/questions/4428795/sqlite-convert-string-to-date“以及:https://www.tutlane.com/tutorial/sqlite/sqlite-date-function#:~:text=语法%20(属于%20SQLite%20date(),日期时间%20)将%20value%20字符串转换为%20date。&text=语法%20SQLite%20date()%20函数,YYYY%2DMM%2DDD%20格式。“ 您的WHERE子句如下所示:
WHERE
DATE(f.checkin) = ? OR DATE(f.checkout) = ?