如何使用Flask,SQLAlchemy或psycopg2从Postgres中的光标获取数据

问题描述 投票:2回答:1

我正在使用Flask并在Postgres数据库上制作程序

CREATE OR REPLACE FUNCTION "public"."   "()
  RETURNS "pg_catalog"."refcursor" AS $BODY$
DECLARE
    ref refcursor;
BEGIN
    OPEN ref FOR select posts.*, users.username, users.name from posts left join users on posts.user_id = users.id;
    RETURN ref;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

在我的SQLAlchemy代码中

connection.execute("SELECT home_data()").fetchall()

它将游标名称home_data作为“未命名的门户1”返回

和psycopg2

conn = psycopg2.connect(host="localhost",database="xxxx", user="xxxx", password="xxxx")

def home_pro():
        cur = conn.cursor()
        return cur.callproc('home_data')

这段代码没有返回。

请帮助我如何从我的程序中获取数据,我在互联网上搜索但没有得到任何东西。

python postgresql sqlalchemy psycopg2
1个回答
2
投票

我得到答案,

   res = cur.callproc('getPerson')
   row = cur.fetchone()

   cur.execute(f'FETCH ALL IN "{row[0]}"')
   results = cur.fetchall()
© www.soinside.com 2019 - 2024. All rights reserved.