开发者

How to get current date and time from DB using SQLAlchemy

开发者 https://www.devze.com 2023-02-01 07:53 出处:网络
I need to retrieve what\'s the current date and time for the database I\'m connected with SQLAlchemy (not date and time of the machine where I\'m running Python code). I\'ve seen this functions, but t

I need to retrieve what's the current date and time for the database I'm connected with SQLAlchemy (not date and time of the machine where I'm running Python code). I've seen this functions, but they don't开发者_开发问答 seem to do what they say:

>>> from sqlalchemy import *
>>> print func.current_date()
CURRENT_DATE
>>> print func.current_timestamp()
CURRENT_TIMESTAMP

Moreover it seems they don't need to be binded to any SQLAlchemy session or engine. It makes no sense...

Thanks!


I foud the solution: these functions cannot be used in the way I used (print...), but need to be called inside of the code that interacts with the database. For instance:

print select([my_table, func.current_date()]).execute()

or assigned to a field in an insert operation. Accidentally I discovered that exists at least a couple of parameters for these functions:

  • type_ that indicates the type of the value to return, I guess
  • bind that indicates a binding to an SQLAlchemy engine

Two examples of use:

func.current_date(type_=types.Date, bind=engine1)
func.current_timestamp(type_=types.Time, bind=engine2)

Anyway my tests seems to say these parameters are not so important.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号