gpt4 book ai didi

python - 如何在 plpython 函数之间传递 int[] 作为参数

转载 作者:行者123 更新时间:2023-11-29 13:56:19 24 4
gpt4 key购买 nike

我正在使用 PostgreSQL 和 plpython 函数。

我有一个函数func1:

CREATE OR REPLACE FUNCTION func1(a integer, b timestamp with time zone, c integer[])
RETURNS SETOF x AS
$BODY$

parts=plpy.execute("SELECT * FROM func2(%s)"%c)

$BODY$
LANGUAGE plpythonu VOLATILE

func1 使用 c 参数调用 func2,该参数是 integer[]

CREATE OR REPLACE FUNCTION func2(v_c integer[])
RETURNS SETOF y AS
$BODY$
.
.
.
$BODY$
LANGUAGE plpythonu VOLATILE

从 SQL 查询运行时

select *
from func1(3,'14-Feb-2012','{-2,30747,30906}')

我收到以下错误:

ERROR:  spiexceptions.SyntaxError: syntax error at or near "["
LINE 1: SELECT * FROM func2([-2, 30747, 30906])

传递 intger[] 的东西不起作用我也试过了:

plpy.execute("SELECT * FROM func2(%s)"%str(c))

它也不起作用。

最佳答案

探索这个:

create or replace function func1(t text[]) returns integer as $$
p = plpy.prepare("select func2($1) v", ["text[]",])
r = plpy.execute(p, [t,])
return r[0]["v"]
$$ language 'plpythonu';

create or replace function func2(t text[]) returns integer as $$
return len(t)
$$ language 'plpythonu';

select func1('{"a", "b"}'::text[]);
> 2

我认为最好的方法是在执行中将列表(postgres 数组)作为参数传递。需要先准备声明。

关于python - 如何在 plpython 函数之间传递 int[] 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com