是否有适当的方法处理Psycopg中的PostgreSQL函数返回的光标?

发布于 2025-01-20 23:30:01 字数 1443 浏览 1 评论 0原文

我正在尝试与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 tool
SELECT 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ左 2025-01-27 23:30:01

您显示的光标链接是指python db api光标,而不是邮政。有一个示例说明如何在此处做您想做的部分:

注意,除了使用execute()执行的声明以外,还可以使用命名光标以其他方式消耗光标。例如,您可能具有PL/PGSQL函数返回光标:

CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS $
BEGIN
    OPEN $1 FOR SELECT col FROM test;
    RETURN $1;
END;
$ LANGUAGE plpgsql;

您可以通过使用常规,未命名的psycopg光标来读取光标内容:

cur1 = conn.cursor()
cur1.callproc('reffunc', ['curname'])

and then use a named cursor in the same transaction to “steal the cursor”:

cur2 = conn.cursor('curname')
for record in cur2:     # or cur2.fetchone, fetchmany...
    # do something with record
    pass

update

确保并关闭name Cursor(CUR2)以释放服务器侧光标。所以:

cur2.close()

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:

Note It is also possible to use a named cursor to consume a cursor created in some other way than using the DECLARE executed by execute(). For example, you may have a PL/pgSQL function returning a cursor:

CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS $
BEGIN
    OPEN $1 FOR SELECT col FROM test;
    RETURN $1;
END;
$ LANGUAGE plpgsql;

You can read the cursor content by calling the function with a regular, non-named, Psycopg cursor:

cur1 = conn.cursor()
cur1.callproc('reffunc', ['curname'])

and then use a named cursor in the same transaction to “steal the cursor”:

cur2 = conn.cursor('curname')
for record in cur2:     # or cur2.fetchone, fetchmany...
    # do something with record
    pass

UPDATE

Be sure and close the named cursor(cur2) to release the server side cursor. So:

cur2.close()
明月松间行 2025-01-27 23:30:01

之前的答案对我来说不起作用。下面是有效的。第一个存储函数(注意,没有 refcursor 参数):

CREATE FUNCTION func() RETURNS refcursor AS $
DECLARE
    ref refcursor;
BEGIN
    OPEN ref FOR SELECT col FROM test;
    RETURN ref;
END;
$ LANGUAGE plpgsql;

然后按如下方式调用它(cursor 返回带有 refcursor 名称的行):

cur = conn.cursor()
cur.callproc("func", [])
ref_cursor_name = cur.fetchone()[0]
ref_cursor = conn.cursor(ref_cursor_name)
data = ref_cursor.fetchall()

Previous answer didn't work in my case. Below is the one which worked. First stored function (note, there is no refcursor parameter):

CREATE FUNCTION func() RETURNS refcursor AS $
DECLARE
    ref refcursor;
BEGIN
    OPEN ref FOR SELECT col FROM test;
    RETURN ref;
END;
$ LANGUAGE plpgsql;

And then you call it as follows (cursor returns a row with refcursor name):

cur = conn.cursor()
cur.callproc("func", [])
ref_cursor_name = cur.fetchone()[0]
ref_cursor = conn.cursor(ref_cursor_name)
data = ref_cursor.fetchall()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文