How could i get the sqlite date delta in SQLAlchemy -
i have sqlite table column final_date in integer type (use date). want calculate number of days column now. in sqlite, can run sql of function (julianday) following.
select (julianday(final_date) - julianday('now')) days bond days > 0 109.407374872826 482.407374872826 488.407374872826 ....
however, in sqlalchemy, code following can not accurate number of days.
today = datetime.today().date()
query = session.query( bond.final_date - today )
i wonder whether there effective way apply function julianday sqlalchemy code or whether there alternative native approach in sqlalchemy accomplish this.
if have control on database , data - may want update schema define column sqlalchemy.types.datetime
. use datetime
module make calculations dates.
or can use sqlalchemy.sql.func
access julianday() sqlite function.
from sqlalchemy.sql import func q = session.query( func.julianday( bond.final_date) - func.julianday("now")) row in q.all(): print row[0]
please note second approach uses sqlite specific function , won't work other database systems.
Comments
Post a Comment