我正在使用 PL/pgSQL 尝试模拟我在 Oracle PL/SQL 中可以执行的操作,其中 dbms_output 相当于 stdout。
我读过“RAISE NOTICE”可能是处理此问题的最佳方法。然而我的问题是,当我从 psycopg2 通知对象检索文本时,我得到了额外的 NOTICE: 前缀和额外的换行符。
我知道我可以从字符串中删除有问题的字符,但它看起来有点笨拙,而且我不知道如何检索原始文本,这让我很恼火。是否可以?
DECLARE retval smallint;
BEGIN
SELECT value INTO retval
FROM montb;
Raise notice 'This is a message';
Raise notice 'This is another message';
RETURN retval;
END;
#!/usr/bin/python
import psycopg2
try:
conn = psycopg2.connect("dbname='mondb' user='nagios' host='postgres' password='nagios'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
mynotices = list()
conn.notices = mynotices
cur.execute('select check_table()')
retval = cur.fetchone()[0]
cur.close()
for notice in mynotices:
print notice
conn.close()
print retval
root@95c2a4abcd95:~# ./test.py
NOTICE: This is a message
NOTICE: This is another message
0
驱动程序从服务器获取带有前缀和行尾字符的警告文本。它不处理该消息。解决方法非常简单:
for notice in mynotices:
print notice[9:-1]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)