Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python2 print postgresql stored procedure raise notice

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

like image 809
Derek Lee Avatar asked Mar 07 '23 06:03

Derek Lee


1 Answers

Try using 'notices'

print(conn.notices)

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

like image 169
VynlJunkie Avatar answered Mar 23 '23 04:03

VynlJunkie