是否有适当的方法处理Psycopg中的PostgreSQL函数返回的光标?
我正在尝试与PostgreSQL(在Windows上构建1914 64位),Psycopg2(使用PIP安装的2.9.1)和Windows上的Python 3.8.10结交朋友。
我已经在数据库中创建了一个返回光标的PostgreSQL函数,
CREATE get_rows
...
RETURNS refcursor
...
DECLARE
res1 refcursor;
BEGIN
OPEN res1 FOR
SELECT some_field, and_another_field FROM some_table;
RETURN res1;
END
可以从PGADMIN4 QUERT工具运行该功能 下方的内容 选择get_rows();
然后将返回一个光标,例如“<未命名门户1>”
仍然在PGADMIN4中的查询工具中,我可以发表:
BEGIN;
SELECT get_rows();
FETCH ALL IN "<unnamed portal 2>"; -- Adjust counter number
这将使我的行返回光标。
现在,我想使用psycopg而不是pgadmin4复制此内容,
我有以下代码
conn = psycopg2.connect("dbname='" + db_name + "' "\
"user='" + db_user + "' " +\
"host='" + db_host + "' "+\
"password='" + db_passwd + "'")
cursor = conn.cursor()
cursor.callproc('get_rows')
print("cursor.description: ", end = '')
print(cursor.description)
for record in cursor:
print("record: ", end = '')
print (record)
在Psycopg创建的光标的单个记录中,仅提供光标字符串名称(如PostgreSQL函数'get_rows'返回)。
如何从psycopg获得光标级对象,以提供“ get_rows”返回的光标?
https://www.psycopg.org/docs/docs/cursor.html cursor。名称是只读的,我看不到一种明显的方法可以将光标从“ get_rows”与psycopg光标内置连接起来
I'm trying to make friends with postgresql (14.0 build 1914 64-bit on windows), psycopg2 (2.9.1 installed using pip) and python 3.8.10 on windows.
I have created a postgresql function in a database that returns a cursor, somthing like below
CREATE get_rows
...
RETURNS refcursor
...
DECLARE
res1 refcursor;
BEGIN
OPEN res1 FOR
SELECT some_field, and_another_field FROM some_table;
RETURN res1;
END
The function can be run from pgAdmin4 Quert toolSELECT get_rows();
and will then return a cursor like "<unnamed portal 1>"
Still within query tool in pgAdmin4 I can issue:
BEGIN;
SELECT get_rows();
FETCH ALL IN "<unnamed portal 2>"; -- Adjust counter number
And this will get me the rows returned by the cursor.
Now I want to replicate this using psycopg instead of pgAdmin4
I have the below code
conn = psycopg2.connect("dbname='" + db_name + "' "\
"user='" + db_user + "' " +\
"host='" + db_host + "' "+\
"password='" + db_passwd + "'")
cursor = conn.cursor()
cursor.callproc('get_rows')
print("cursor.description: ", end = '')
print(cursor.description)
for record in cursor:
print("record: ", end = '')
print (record)
The above code only gives the cursor string name (as returned by the postgresql function 'get_rows') in the single record of the cursor created by psycopg.
How can I get a cursor-class object from psycopg that provides access the cursor returned by 'get_rows'?
https://www.psycopg.org/docs/cursor.html says cursor.name is read-only and I dont see an obvious way to connect the cursor from 'get_rows' with a psycopg cursor-instance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您显示的光标链接是指python db api光标,而不是邮政。有一个示例说明如何在此处做您想做的部分:
update
确保并关闭
name Cursor
(CUR2)以释放服务器侧光标。所以:The cursor link you show refers to the Python DB API cursor not the Postgres one. There is an example of how to do what you want here Server side cursor in section:
UPDATE
Be sure and close the
named cursor
(cur2) to release the server side cursor. So:之前的答案对我来说不起作用。下面是有效的。第一个存储函数(注意,没有 refcursor 参数):
然后按如下方式调用它(cursor 返回带有 refcursor 名称的行):
Previous answer didn't work in my case. Below is the one which worked. First stored function (note, there is no refcursor parameter):
And then you call it as follows (cursor returns a row with refcursor name):