【问题标题】:How to get current date and time from DB using SQLAlchemy如何使用 SQLAlchemy 从数据库中获取当前日期和时间
【发布时间】:2011-05-31 22:39:06
【问题描述】:
我需要使用 SQLAlchemy 检索我连接的数据库的当前日期和时间(不是我运行 Python 代码的机器的日期和时间)。我见过这个函数,但他们似乎并没有按照他们说的去做:
>>> from sqlalchemy import *
>>> print func.current_date()
CURRENT_DATE
>>> print func.current_timestamp()
CURRENT_TIMESTAMP
此外,它们似乎不需要绑定到任何 SQLAlchemy 会话或引擎。没有意义……
谢谢!
【问题讨论】:
标签:
python
database
date
time
sqlalchemy
【解决方案1】:
我找到了解决方案:这些函数不能以我使用的方式使用(print...),而是需要在与数据库交互的代码内部调用。例如:
print select([my_table, func.current_date()]).execute()
或分配给插入操作中的字段。
偶然我发现这些函数至少存在几个参数:
-
type_ 表示要返回的值的类型,我猜
-
bind 表示绑定到 SQLAlchemy 引擎
两个使用示例:
func.current_date(type_=types.Date, bind=engine1)
func.current_timestamp(type_=types.Time, bind=engine2)
无论如何,我的测试似乎表明这些参数并不那么重要。