在sqlalchemy中使用DATEADD [英] Using DATEADD in sqlalchemy
本文介绍了在sqlalchemy中使用DATEADD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能用python中的sqlalchemy重写下面的sql语句。我一直在寻找30分钟,但仍然找不到任何解决方案。
DATEADD(NOW(),INTERVAL 1 DAY)
或
INSERT INTO日期(过期)
VALUES(DATEADD(NOW(),INTERVAL 1 DAY))
提前致谢解决方案SQLAlchemy的日期自动映射到Python日期时间对象,所以您应该能够
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
元数据元数据()
示例=表('users',元数据,
列('expire',DateTime)
)
明天= datetime.now()+ timedelta(days = 1)
ins = example.insert()。values(expire = tomorrow)
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
解决方案
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)
这篇关于在sqlalchemy中使用DATEADD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文