SQLite3 Query in Python to Retrieve count of rows between two Dates
Solution 1:
The problem with your code is that you have set the starting and the ending values of the operator BETWEEN
reversed.
It should be:
BETWEEN datetime('{}', '-15 day') AND datetime('{}')
but, I would also suggest to use ?
placeholders to pass the parameters.
Also, there is no need to use the datetime()
or date()
functions to get a date if it is already in the format YYYYY-MM-DD
.
Use this:
sql = "SELECT COUNT(*) FROM bt WHERE id = ? AND btdate BETWEEN date(?, '-15 day') AND ?;"
cur.execute(sql, (row['id'], row['date'], row['date']))