libpq: How to get the error code after a failed PGconn connection

烈酒焚心 提交于 2020-01-01 15:09:16

问题


Given the following code.

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).

Thanks.


回答1:


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().

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).

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

TL;DR: If you are going to use libpq, learn to live with the text error message (whatever language it is localized in).




回答2:


Try...

cout << PQerrorMessage(conn) << endl;


来源:https://stackoverflow.com/questions/23349086/libpq-how-to-get-the-error-code-after-a-failed-pgconn-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!