简体   繁体   English

Python SQLAlchemy - TypeError:“type”和“datetime.datetime”的实例之间不支持“>”

[英]Python SQLAlchemy - TypeError: '>' not supported between instances of 'type' and 'datetime.datetime'

I have a query that looks at a table in my MySQL database and I'm looking to retrieve the data between two datetimes.我有一个查询,它查看我的 MySQL 数据库中的一个表,我希望检索两个日期时间之间的数据。
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但是,对我来说奇怪的是,当我检查查询实例中的第一组数据时,它datetime.datetime 格式

>>> 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?有没有人对我如何使用 datetime.datetime 类型按日期时间进行过滤有任何建议?

I think datetime is recognize as type not table column name in query.filter_by(datetime > start_date) code.我认为datetimequery.filter_by(datetime > start_date)代码中被识别为type而不是表列名。
Use filter instead of filter_by使用filter而不是filter_by

query.filter(Table.datetime > start_date)

face palm脸掌
Wasn't too smart using datetime as the column header in the MySQL database使用日期时间作为 MySQL 数据库中的列标题不太聪明
I was able to get the filter by using the table.c operator:我能够通过使用 table.c 运算符来获取过滤器:

>>> query.filter(table.c.datetime > start_date)
<sqlalchemy.orm.query.Query object at 0x000001EC6502C940>

Note, I used query.filter instead of query.filter_by注意,我使用了query.filter而不是query.filter_by

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:“datetime.datetime”和“float”实例之间不支持“&gt;=” - TypeError: '>=' not supported between instances of 'datetime.datetime' and ' float' TypeError:在“ datetime.datetime”和“ str”的实例之间不支持“&gt;” - TypeError: '>' not supported between instances of 'datetime.datetime' and 'str' “TypeError: '&gt;' 在 'datetime.datetime' 和 'str' 的实例之间不支持” - “TypeError: '>' not supported between instances of 'datetime.datetime' and 'str'” 在Python中合并和转置列会产生TypeError:&#39;int&#39;和&#39;datetime.datetime&#39;实例之间不支持&#39;&gt;&#39; - Merging and transposing columns in Python gives TypeError: '>' not supported between instances of 'int' and 'datetime.datetime' 我是 python 的新手,这是 TypeError:'&lt;=' not supported between 'datetime.datetime' 和 'module' - I am new to python and This is the TypeError:'<=' not supported between instances of 'datetime.datetime' and 'module' 在“ datetime.time”和“ datetime.datetime”的实例之间不支持“&gt;” - '>' not supported between instances of 'datetime.time' and 'datetime.datetime' 在“ datetime.datetime”和“ datetime.time”的实例之间不支持“&gt; =” - '>=' not supported between instances of 'datetime.datetime' and 'datetime.time' TypeError:“ builtin_function_or_method”实例与“ datetime.datetime”实例之间不支持“ &lt;” - TypeError: '<' not supported between instances of 'builtin_function_or_method' and 'datetime.datetime' 烧瓶会话类型错误:在“NoneType”和“datetime.datetime”的实例之间不支持“&lt;=” - flask-session TypeError: '<=' not supported between instances of 'NoneType' and 'datetime.datetime' 尝试获取最小日期并获取 TypeError: &#39;&lt;&#39; 在 &#39;datetime.datetime&#39; 和 &#39;int&#39; 的实例之间不支持 - Trying to get the minimum date and getting TypeError: '<' not supported between instances of 'datetime.datetime' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM