Reputation: 155
I have a query that looks at a table in my MySQL database and I'm looking to retrieve the data between two datetimes.
At the moment, I get the session and the Table properties assigned just fine:
>>> session
<sqlalchemy.orm.session.Session object at 0x000001EC64FF6EF0>
>>> table
Table('txt_raw1', MetaData(bind=None), Column('datetime', DATETIME(fsp=6), table=<txt_raw1>, nullable=False), and some extra stuff...
I then begin to form the query and so far, so good:
>>> query = session.query(table)
>>> query.filter_by(Name='Jose')
>>> query
<sqlalchemy.orm.query.Query object at 0x000001EC50281A90>
However, when I try to filter by the datetime column, I get an error:
>>> start_date = datetime.strptime('2020-11-10','%Y-%m-%d')
>>> start_date
datetime.datetime(2020, 11, 10, 0, 0)
query.filter_by(datetime > start_date)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'type' and 'datetime.datetime'
Odd thing to me, though, when I checked the first set of data in the query instance, it is of the datetime.datetime format
>>> query[0][0]
datetime.datetime(2019, 11, 16, 1, 43, 54, 192000)
Does anyone have any suggestions to how I can filter by datetime using the type datetime.datetime?
Upvotes: 0
Views: 1480
Reputation: 685
I think datetime
is recognize as type
not table column name in query.filter_by(datetime > start_date)
code.
Use filter
instead of filter_by
query.filter(Table.datetime > start_date)
Upvotes: 1
Reputation: 155
face palm
Wasn't too smart using datetime as the column header in the MySQL database
I was able to get the filter by using the table.c operator:
>>> query.filter(table.c.datetime > start_date)
<sqlalchemy.orm.query.Query object at 0x000001EC6502C940>
Note, I used query.filter
instead of query.filter_by
Upvotes: 0