1

Convoluted title. I am using python sqlalchemy with a postgresql database.

I have a database that stores Events, and each event record has a starttime and a endtime column. Both of the aforementioned columns are in this format:

2015-06-22 13:00:00

How can I therefore query the database for all Events from a specific day. Thereby ignoring the "time" part of the starttime and endtime columns?

For example I tried:

query = Event.query.filter(Event.starttime=='2015-06-22', Event.endtime=='2015-06-22').all()

However this came up with 0 results, I am assuming perhaps this is because the absence of hh/mm/ss makes the query 'assume' that I mean 00:00:00.

I also tried

query = Event.query.filter(Event.starttime>='2015-06-22', Event.endtime<='2015-06-22').all()

But this came up with 0 results as well, I assume for the same reason as the previous.

Any help would be appreciated!

This is the query that gets sent to the DB:

SELECT events.id AS events_id, events.summary AS events_summary, events.starttime AS events_starttime, events.endtime AS events_endtime, events.parentcal AS events_parentcal, events.userid AS events_userid 
FROM events 
WHERE events.starttime >= :starttime_1 AND events.endtime < :endtime_1
k4kuz0
  • 1,045
  • 1
  • 10
  • 24

1 Answers1

0

Try using actual datetime objects. SQLAlchemy can interpret them.

from datetime import datetime

starttime = datetime.strptime('2015-06-22', '%Y-%m-%d')
endtime = datetime.strptime('2015-06-23', '%Y-%m-%d')
query = Event.query.filter(Event.starttime>=starttime, Event.endtime<endtime).all()

For more information on parsing datetime strings check out the docs

Tim Martin
  • 2,419
  • 1
  • 21
  • 25