Python2打印postgresql存储过程引发的通知[英] Python2 print postgresql stored procedure raise notice

本文是小编为大家收集整理的关于Python2打印postgresql存储过程引发的通知的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何在Python脚本上打印Postgres的存储过程?

Postgres中存储过程的示例如下:

create or replace function checktime() returns void
language plpgsql
as $$
DECLARE timestart TIMESTAMP;

  FOR id from rt LOOP
    SELECT timeofday() into timestart;
    RAISE NOTICE 'Time now : %', timestart;
  END LOOP;

END;
$$
;

来自Python,我的脚本是:

import psycopg2
conn = psycopg2.connect(host="", database="", 
user="", password="")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

cur.callproc('rt_visits_function_gz')
# how can i display the raise notice in here?

我想要每个循环时,当我运行python时会显示结果.

谢谢

推荐答案

尝试使用'Notices'

打印(conn.notices)

Connection.html?亮点=注意#连接.notices

本文地址:https://www.itbaoku.cn/post/1763945.html

问题描述

How can I print postgres's stored procedure on python script?

Example of stored procedure in postgres is as below:

create or replace function checktime() returns void
language plpgsql
as $$
DECLARE timestart TIMESTAMP;

  FOR id from rt LOOP
    SELECT timeofday() into timestart;
    RAISE NOTICE 'Time now : %', timestart;
  END LOOP;

END;
$$
;

From python, my script is:

import psycopg2
conn = psycopg2.connect(host="", database="", 
user="", password="")
print("Database Connected")
cur = conn.cursor()
rowcount = cur.rowcount

cur.callproc('rt_visits_function_gz')
# how can i display the raise notice in here?

I would like for each loop the result is displayed when i run python.

Thank you

推荐答案

Try using 'notices'

print(conn.notices)

http://initd.org/psycopg/docs/connection.html?highlight=notice#connection.notices