postgreSQL pl/python 如何从查询中获取返回值?
在这个(非常精简的)查询中,如何从 RETURNING 返回 myfile_key?
CREATE OR REPLACE FUNCTION myfunction (src int, OUT result int) AS $$
plan = plpy.prepare("INSERT INTO myfile VALUES (nextval('seq_myfile'),$1,$2)
RETURNING myfile_key", ["integer","integer"] )
$$ LANGUAGE plpythonu;
(其中插入的第一个字段是 myfile_key)
代码不会将值返回到输出,并且无法将其作为标准 python 结果进行查询,如下所示:
result = rv[0]["myfile_key"]
in this (vey condensed) query, how would one return the myfile_key from RETURNING?
CREATE OR REPLACE FUNCTION myfunction (src int, OUT result int) AS $
plan = plpy.prepare("INSERT INTO myfile VALUES (nextval('seq_myfile'),$1,$2)
RETURNING myfile_key", ["integer","integer"] )
$ LANGUAGE plpythonu;
(where the first field inserted is myfile_key)
The code doesn't return the value to output, and haven't been able to query it as a standard python result, as in:
result = rv[0]["myfile_key"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的代码中缺少对 plpy.execute 的调用。原则上您有权访问
RETURNING
列,例如rv[0]["myfile_key"]
。不起作用的是 PL/Python 中对 OUT 参数的赋值。我建议您使用普通的return
语句重写不带OUT
参数的函数。I assume there is a call to
plpy.execute
missing in your code. You are in principle right to access theRETURNING
columns likerv[0]["myfile_key"]
. What's not working is the assignment toOUT
parameters in PL/Python. I suggest that you rewrite your function withoutOUT
parameters using a normalreturn
statement.