【问题标题】:How To Call Postgres 11 Stored Procedure From Python如何从 Python 调用 Postgres 11 存储过程
【发布时间】:2019-07-26 18:06:12
【问题描述】:

我在 Postgres 中有一个名为 sales 的存储过程,它在 pgadmin 上运行良好:

CALL sales();

但是,当我从 Python 调用它时:

import psycopg2
conn = psycopg2.connect (host ....)
cur = conn.cursor()
cur.callproc('sales')
conn.commit()

我收到以下错误消息:

psycopg2.ProgrammingError: sales() is a procedure
LINE 1: SELECT * FROM sales()
                 ^   
HINT:  To call a procedure, use CALL.

【问题讨论】:

    标签: python python-3.x postgresql stored-procedures psycopg2


    【解决方案1】:

    假设您的程序称为销售,您只需要“调用”它,例如CALL sales()

    https://www.postgresql.org/docs/11/sql-call.html

    我明白你在说什么,这里的 python 文档具有误导性

    “在 Python 步骤中调用 PostgreSQL 存储过程” http://www.postgresqltutorial.com/postgresql-python/call-stored-procedures/

    基本上 callproc 当前已过时(为 postgres 10 及以下版本编写)并且仍将过程视为函数。因此,除非他们更新此内容,否则您将需要像这样在此实例中执行自己的 SQL

    cur.execute("CALL sales();")

    或者如果销售程序需要输入:

    cur.execute("CALL sales(%s, %s);", (val1, val2))

    【讨论】:

    • 如何从 python 脚本调用... cur.callproc
    • 我已经更新了答案,希望现在有一些用处。
    • 谢谢卢卡斯!它完美地工作。你祝福我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2010-09-22
    • 2018-04-23
    相关资源
    最近更新 更多