[英]SQLite return records where date is more than a week old
I am making a program for a book library and in the sqlite database there is a table for the books which includes the date that book was last taken out. 我正在为图书库制作一个程序,在sqlite数据库中有一张图书表,其中包括该书的最后取出日期。
I need a query to show overdue books. 我需要查询以显示过期的书。 My code so far follows:
到目前为止,我的代码如下:
def findOverdueBooks(event):
findRecords = c.execute("SELECT * FROM bookList WHERE takenOut < 'now' , '-1 week' " )
for row in findRecords:
print(row)
I am getting this error when the code is run 运行代码时出现此错误
line 31, in findOverdueBooks
findRecords = c.execute("SELECT * FROM bookList WHERE takenOut < 'now' , '-1 week' " )
sqlite3.OperationalError: near ",": syntax error
I don't understand why the comma is causing an error as that is how it is shown in the documentation . 我不明白为什么逗号会导致错误,因为这就是文档中所显示的方式。
Your datetime comparison is causing the issue here. 您的日期时间比较导致此问题。 It rather should be
它应该是
AND takenOut < datetime('now', '-7 day')
See SQLite Date And Time Functions for more information 有关更多信息,请参见SQLite日期和时间函数 。
That should be a >
comparison 那应该是
>
比较
AND takenOut > datetime('now', '-7 day')
You can as well try like below, if you are comparing with the date part only 如果仅与日期部分进行比较,也可以尝试如下所示
WHERE DATE(takenOut) >= DATE('now', 'weekday 0', '-7 days')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.