Alper Alper - 3 months ago 48
Python Question

Using DATEADD in sqlalchemy

How can I rewrite the following sql statement with sqlalchemy in python. I have been searching for 30 mins but still couldn't find any solutions.

DATEADD(NOW(), INTERVAL 1 DAY)


or

INSERT INTO dates (expire)
VALUES(DATEADD(NOW(), INTERVAL 1 DAY))


Thanks in advance

Answer

SQLAlchemy dates automagically map to Python datetime objects, so you should just be able to do:

from sqlalchemy import Table, Column, MetaData, DateTime
from datetime import datetime, timedelta

metadata = MetaData()
example = Table('users', metadata,
   Column('expire', DateTime)
)

tomorrow = datetime.now() + timedelta(days=1)

ins = example.insert().values(expire=tomorrow)
Comments