在 sqlalchemy 中使用 DATEADD [英] Using DATEADD in sqlalchemy

查看:33
本文介绍了在 sqlalchemy 中使用 DATEADD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中用sqlalchemy重写下面的sql语句.我已经搜索了 30 分钟,但仍然找不到任何解决方案.

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)

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

提前致谢

推荐答案

SQLAlchemy 日期自动映射到 Python 日期时间对象,因此您应该能够:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆