我想通过 psycopg2 调用 plpgsql 函数并查看警告消息。
即,我有这个功能:
create or replace function test_warning() returns void as $$
begin
raise warning 'this is only a test';
end;
$$
language plpgsql;
并在 python 中这样称呼它:
import psycopg2
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.callproc("test_warning")
# or so:
cursor.execute('SELECT test_warning()')
不幸的是,plpgsql 中定义的警告消息没有出现在 python 输出中的任何位置。
有没有办法在 python 输出中打印警告消息?
The notices
的成员connection
是截至该点发送到客户端的会话消息的列表:
for notice in conn.notices:
print notice
http://initd.org/psycopg/docs/connection.html#connection.notices http://initd.org/psycopg/docs/connection.html#connection.notices
要获取最后通知:
print conn.notices[-1]
如果函数内部引发异常且未被捕获,则不会收到警告。这是因为函数包装了隐式事务,并且该事务内的所有内容都会回滚,包括警告。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)