我正在使用PL/pgSQL,试图模拟我在oraclepl/SQL中可以做的事情,并将dbms_输出作为与stdout等价的输出。
我读过,提高通知可能是处理这件事的最好办法。然而,我的问题是,当我从psycopg2 notices对象检索文本时,我得到了额外的注意:前缀和额外的换行符。
我知道我可以从字符串中删除有问题的字符,但是看起来有点笨拙,而且我不知道如何检索原始文本,这让我很恼火。有可能吗?
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]