调用Python使用PostgreSQL存储过程。

huangapple go评论112阅读模式
英文:

Call Postgresql Proocedure using python

问题

尝试使用Python运行PostgreSQL存储过程,但无法纠正错误。。

  1. def call_procedure_without_arguments():
  2. try:
  3. connection = create_connection()
  4. if connection:
  5. cursor = connection.cursor()
  6. # 调用没有输入参数的存储过程
  7. procname = "call proc_test"
  8. cursor.callproc(procname)
  9. results = cursor.fetchall()
  10. print("Results:", results)
  11. # 提交更改(如果有)
  12. connection.commit()
  13. # 关闭游标和连接
  14. cursor.close()
  15. connection.close()
  16. print("存储过程成功执行。")
  17. else:
  18. print("连接失败。无法调用存储过程。")
  19. except Exception as e:
  20. print("错误:无法调用存储过程。", e)
  21. if __name__ == "__main__":
  22. call_procedure_without_arguments()
英文:

Trying to run a postgresql procedure using python but not able to rectify the error..

  1. def call_procedure_without_arguments():
  2. try:
  3. connection = create_connection()
  4. if connection:
  5. cursor = connection.cursor()
  6. # Call the procedure without any input arguments
  7. procname = "call proc_test"
  8. cursor.callproc(procname)
  9. results = cursor.fetchall()
  10. print("Results:", results)
  11. # Commit the changes (if any)
  12. connection.commit()
  13. # Close the cursor and connection
  14. cursor.close()
  15. connection.close()
  16. print("Procedure executed successfully.")
  17. else:
  18. print("Connection failed. Unable to call the procedure.")
  19. except Exception as e:
  20. print("Error: Unable to call the procedure.", e)
  21. if __name__ == "__main__":
  22. call_procedure_without_arguments()

答案1

得分: 0

根据Geeks for Geeks的文档:

  1. engine = connector.cursor()
  2. engine.callproc('your_procedure')
  3. result = cursor.fetchall()

如果不起作用,请尝试添加括号 (),根据Postgresql Tutorial

  1. conn = psycopg2.connect(dsn)
  2. cur.execute("CALL your_procedure();")
  3. 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

  1. engine = connector.cursor()
  2. engine.callproc('your_procedure')
  3. result = cursor.fetchall()

If does not work try adding parenthesis (), according to Postgresql Tutorial:

  1. conn = psycopg2.connect(dsn)
  2. cur.execute("CALL your_procedure();")
  3. 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')

huangapple
  • 本文由 发表于 2023年7月24日 15:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定