Using DATEADD in sqlalchemy

后端 未结 5 1760
面向向阳花
面向向阳花 2020-12-03 18:03

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(NO         


        
5条回答
  •  無奈伤痛
    2020-12-03 18:46

    For completeness sake, here is how you'd generate that exact SQL with using sqlalchemy.sql.func:

    from sqlalchemy.sql import func
    from sqlalchemy.sql.expression import bindparam
    from sqlalchemy import Interval
    
    tomorrow = func.dateadd(func.now(), bindparam('tomorrow', timedelta(days=1), Interval()))
    

    which results in:

    >>> from sqlalchemy.sql import func
    >>> func.dateadd(func.now(), bindparam('tomorrow', timedelta(days=1), Interval(native=True)))
    
    >>> str(func.dateadd(func.now(), bindparam('tomorrow', timedelta(days=1), Interval(native=True))))
    'dateadd(now(), :tomorrow)'
    

    Alternatively you could use a text() object to specify the interval instead:

    from sqlalchemy.sql import func
    from sqlalchemy.sql.expression import text
    
    tomorrow = func.dateadd(func.now(), text('interval 1 day'))
    

提交回复
热议问题