libpq:PGconn连接失败后如何获取错误代码 [英] libpq: How to get the error code after a failed PGconn connection

查看:308
本文介绍了libpq:PGconn连接失败后如何获取错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码.

PGconn* const conn=PQconnectdbParams(keywords, values, false);
if(! conn || PQstatus(conn)!=CONNECTION_OK){ /* error code? */ }

在连接失败的情况下,有一种方法可以获取错误代码,以便能够 区分密码错误和服务器已关闭.

In case of a failed connection is there a way to get the error code to be able to distinguish between a bad password and the server being down.

(我知道我会收到错误消息,但我希望能够对导致错误的原因做出反应 错误(根据其原因).

(I know I can get the error message, but I want to be able to react to the cause of the error according to its cause).

谢谢.

推荐答案

因此,在进行了更多研究并询问postgres开发人员之后,似乎目前无法从PQconnectdbParams()获取错误代码.

So after doing some more research and asking postgres developers it seems that it is not currently possible to get the error code from PQconnectdbParams().

这似乎是libpq本身的缺点,它不尝试为其内部检测到的错误(例如未知的主机名)分配SQLState错误代码.

This seems to be a shortcoming of libpq itself which doesn't try to assign SQLState error codes for errors it detects internally (like an unknown hostname).

这似乎在postgres dev待办事项列表上,但这不是优先事项.

This seems to be on the postgres dev todo list but it's not a priority.

TL; DR:如果要使用libpq,请学习如何处理文本错误消息(无论其本地化为哪种语言).

这篇关于libpq:PGconn连接失败后如何获取错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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