2014-07-08 117 views
0

我有一個LocationEvent模型,它具有timestamp屬性,它是DateTime字段。我想寫一個函數,它將採用start_timeend_time(它們是python time對象),並將返回在這兩個時間之間的對象 - 無論日期如何。使用SqlAlchemy + python在日期時間字段上按時過濾

我嘗試使用SQLAlchemy中的extract函數,但無法成功。我嘗試以下 -

LocationEvent.query.filter(sqlalchemy.extract('time', LocationEvent.timestamp) >= start_time) 

這給了我下面的錯誤 -

sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL 
syntax; check the manual that corresponds to your MySQL server version for the right syntax to 
use near 'time FROM location_event.timestamp) >= '08:00:00'' at line 3") 'SELECT 
location_event.id AS location_event_id, location_event.latitude AS location_event_latitude, 
location_event.longitude AS location_event_longitude, location_event.type AS 
location_event_type, location_event.timestamp AS location_event_timestamp, 
location_event.creation_date AS location_event_creation_date \nFROM location_event \nWHERE 
EXTRACT(time FROM location_event.timestamp) >= %s' ('08:00:00',) 

我如何能做到這一點任何想法?

+0

'EXTRACT'不是由SQL Server的支持。 – zsong

回答

2

我能做到這一點使用下面的 -

from sqlalchemy import Time, cast 
LocationEvent.query.filter(cast(LocationEvent.timestamp, Time) >= start_time, cast(LocationEvent.timestamp, Time) <= end_time).all() 
相關問題