在 PQconnectdb C++代码块中中止(核心转储)

Aborted (core dumped) in PQconnectdb C++ Codeblocks

本文关键字:核心 转储 C++ 代码 PQconnectdb      更新时间:2023-10-16

我的代码块插件有这样的代码:

 conn = PQconnectdb("dbname=codeblocks user=postgres password=postgres host=127.0.0.1 port=5432");
if(PQstatus(conn) != CONNECTION_OK) {
    wxMessageBox(_("Database connection failed!" ));
    PQfinish(conn);
    return 0;
}

在此基础上,与数据库的连接将出错,因为它不存在,并且将显示"数据库连接失败!"消息。我的问题是,它关闭了代码块IDE并在我的终端中显示错误,如下所示:

Exception: Unknown exception was raised. The application will terminate immediately...
Aborted (core dumped)

我怎样才能在 PQconnectdb 上捕获错误,以便它不会关闭我的 IDE?顺便说一句,我在 Ubuntu 14.04 中使用C++。

首先,您可以将您起诉的代码包含在 try-catch 中:

try{
  conn = PQconnectdb("dbname=codeblocks user=postgres password=postgres host=127.0.0.1 port=5432");
  if(PQstatus(conn) != CONNECTION_OK) {
    wxMessageBox(_("Database connection failed!" ));
    PQfinish(conn);
    return 0;
  }
} catch (...) {
  /* add breakpoint here */
}

此外,您可以检查您是否安装了多个版本的代码块。

  • 数据库存在吗?
  • postgresql 是否已安装?
  • 您是否使用了正确的主机和/或端口?