英文:
Call Postgresql Proocedure using python
问题
尝试使用Python运行PostgreSQL存储过程,但无法纠正错误。。
def call_procedure_without_arguments():
try:
connection = create_connection()
if connection:
cursor = connection.cursor()
# 调用没有输入参数的存储过程
procname = "call proc_test"
cursor.callproc(procname)
results = cursor.fetchall()
print("Results:", results)
# 提交更改(如果有)
connection.commit()
# 关闭游标和连接
cursor.close()
connection.close()
print("存储过程成功执行。")
else:
print("连接失败。无法调用存储过程。")
except Exception as e:
print("错误:无法调用存储过程。", e)
if __name__ == "__main__":
call_procedure_without_arguments()
英文:
Trying to run a postgresql procedure using python but not able to rectify the error..
def call_procedure_without_arguments():
try:
connection = create_connection()
if connection:
cursor = connection.cursor()
# Call the procedure without any input arguments
procname = "call proc_test"
cursor.callproc(procname)
results = cursor.fetchall()
print("Results:", results)
# Commit the changes (if any)
connection.commit()
# Close the cursor and connection
cursor.close()
connection.close()
print("Procedure executed successfully.")
else:
print("Connection failed. Unable to call the procedure.")
except Exception as e:
print("Error: Unable to call the procedure.", e)
if __name__ == "__main__":
call_procedure_without_arguments()
答案1
得分: 0
根据Geeks for Geeks的文档:
engine = connector.cursor()
engine.callproc('your_procedure')
result = cursor.fetchall()
如果不起作用,请尝试添加括号 (),根据Postgresql Tutorial:
conn = psycopg2.connect(dsn)
cur.execute("CALL your_procedure();")
conn.commit()
英文:
I am expecting that no arguments need to be passed. The documentation for this is different at different resources.
According to Geeks for Geeks
engine = connector.cursor()
engine.callproc('your_procedure')
result = cursor.fetchall()
If does not work try adding parenthesis (), according to Postgresql Tutorial:
conn = psycopg2.connect(dsn)
cur.execute("CALL your_procedure();")
conn.commit();
答案2
得分: 0
与名称callproc
相反,它不适用于存储过程。它仅适用于函数:
https://www.psycopg.org/docs/cursor.html#cursor.callproc
注意
callproc()
只能用于PostgreSQL函数,不能用于PostgreSQL 11中引入的存储过程,这些存储过程需要使用CALL
语句来运行。请使用普通的execute()
来运行它们。
因此:
cur.execute('call proc_test')
英文:
Contrary to the name callproc
is not for procedures. It is for functions only:
https://www.psycopg.org/docs/cursor.html#cursor.callproc
>Note
>callproc() can only be used with PostgreSQL functions, not with the procedures introduced in PostgreSQL 11, which require the CALL statement to run. Please use a normal execute() to run them.
Therefore:
cur.execute('call proc_test')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论