从PostgreSQL检索原始通知文本 [英] Retreiving original notice text from PostgreSQL

查看:104
本文介绍了从PostgreSQL检索原始通知文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PL/pgSQL尝试用dbms_output等效于stdout来模拟我在Oracle PL/SQL中可以做的事情.

I'm using PL/pgSQL trying to emulate what I can do in Oracle PL/SQL with dbms_output as an equivalent to stdout.

我已经阅读过RAISE NOTICE可能是处理此问题的最佳方法.但是我的问题是,当我从psycopg2 notes对象检索文本时,会得到额外的NOTICE:前缀和额外的换行符.

I have read that RAISE NOTICE is probably the best way to handle this. However my problem is when I retrieve the text from psycopg2 notices object I get the extra NOTICE: prefix and an extra linefeed.

我知道我可以从字符串中删除有问题的字符,但是这似乎有些笨拙,而且令我烦恼的是我不知道如何检索原始文本.有可能吗?

I know I can just remove the problem characters from the string, however it seems a little clunky, and it's annoying me that I can't work out how to retrieve the original text. Is it possible?

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

推荐答案

驱动程序从服务器获取带有前缀和行尾字符的警告文本.它不处理此消息.解决方法非常简单:

The driver gets a warning text from the server with the prefix and the end-of-line character. It does not process this message. The workaround is very simple:

for notice in mynotices:
    print notice[9:-1]

这篇关于从PostgreSQL检索原始通知文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆