Python 从 psycopg2 异常中获取错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24332005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error messages from psycopg2 exceptions
提问by sirjames2004
This is my first project using psycopg2 extensively. I'm trying to find a way to extract the psql error message for whenever a connection attempt fails. I've tested the code below will work if all the variables are set correctly, however whenever an error condition occurs (e.g. user chooses a database that doesn't exist), Python will give me the following:
这是我第一个广泛使用 psycopg2 的项目。我正在尝试找到一种方法来在连接尝试失败时提取 psql 错误消息。如果所有变量设置正确,我已经测试了下面的代码将起作用,但是每当出现错误情况时(例如用户选择了一个不存在的数据库),Python 会给我以下信息:
I am unable to connect to the database
None
Traceback (most recent call last):
File "./duplicate_finder.py", line 163, in <module>
main(sys.argv[1:])
File "./duplicate_finder.py", line 142, in main
print e.diag.message_detail
AttributeError: 'OperationalError' object has no attribute 'diag'
Is there a simple, catch-all method to catch whatever error message psql generates when a connection fails, or do I need to write except blocks for multiple psycopg2 exceptions?
是否有一种简单的、全能的方法来捕获连接失败时 psql 生成的任何错误消息,或者我是否需要为多个 psycopg2 异常编写 except 块?
Extract from my script:
从我的脚本中提取:
import sys, getopt, os, time, csv, psycopg2
...
...
conn_string = "host=" + dbhost + " dbname=" + database + " user=" + dbuser + " password=" + dbpass
try:
conn = psycopg2.connect(conn_string)
except psycopg2.Error as e:
print "Unable to connect!"
print e.pgerror
print e.diag.message_detail
sys.exit(1)
else:
print "Connected!"
cur = conn.cursor()
cur.execute("SELECT id, lastname, firstname, location FROM test ORDER BY ctl_upd_dttm DESC;")
print cur.fetchone()
...
conn.close()
采纳答案by Clodoaldo Neto
You are catching all exceptions with the base class psycopg2.Error
. Your problem is probably that the diag
attribute is new in psycopg2 2.5
. What is your version?
您正在使用基类捕获所有异常psycopg2.Error
。您的问题可能是该diag
属性在psycopg2 2.5
. 你的版本是什么?
>>> print psycopg2.__version__
2.5.1 (dt dec pq3 ext)
回答by sdemurjian
When I try to catch exceptions, e.pgerror is always None for connection errors. The following code block gets around this by directly printing 'e'.
当我尝试捕获异常时,对于连接错误,e.pgerror 始终为 None。下面的代码块通过直接打印“e”来解决这个问题。
try:
conn = psycopg2.connect(conn_string)
except psycopg2.OperationalError as e:
print('Unable to connect!\n{0}').format(e)
sys.exit(1)
else:
print('Connected!')
# do stuff
For example, in the case of password authentication failure:
例如,在密码认证失败的情况下:
Unable to connect!
FATAL: password authentication failed for user "user"
I realize this question is a year old but hopefully might help someone in the future
我意识到这个问题已经有一年了,但希望将来可以帮助某人
回答by Loaderon
Ended up here because of on Django If that's your case, be sure to makemigrations
由于在 Django 上而到这里结束 如果是这样,请务必进行迁移