在postgresql中如何使用datetime实例作为时间戳?

2024-10-01 13:41:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用PL/Python创建postgresql函数(CREATE FUNCTION)。在

如何在过程中将pythondatetime对象转换为postgresqltimestamp对象?在


Tags: 对象函数postgresqlcreatefunctionplpythondatetime过程中将
1条回答
网友
1楼 · 发布于 2024-10-01 13:41:44

告诉prepare方法参数值的类型timestamp,然后将datetime值传递给execute方法:

create or replace function pytest()
returns timestamp as $$

    from datetime import datetime
    plan = plpy.prepare('select $1 as my_datetime', ['timestamp'])
    rv = plpy.execute(plan, (datetime.now(),))
    return rv[0]['my_datetime']

$$ language plpythonu;

select pytest();
           pytest           
              
 2016-07-20 13:57:59.625991

Database Access from PL/Python

相关问题 更多 >