使用C ++连接到Postgres数据库

时间:2014-12-10 22:47:15

标签: c++ postgresql

我正在尝试从Windows桌面连接到Red Hat Linux服务器上的Postgres数据库。我有" Postgres DB ODBC Driver 9.0.0"安装在我的Windows机器上。我使用以下代码。连接不成功。我想知道我的标题是否正常以及我的_szDSN字符串是否正常?

我使用了非常相似的代码来成功连接到我在桌面上创建的本地Access数据库。我还有" Microsoft Access数据库引擎2010"安装在我的桌面上。

如果需要更多信息来回答此问题,请与我们联系。谢谢你的帮助!

#include <iostream>
#include <string>
#include <Windows.h>
#include <sql.h>
#include <sqlext.h>

int main()
{
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN rc;

    //*** Connecting to remote PostgreSQL database
    rc = SQLAllocEnv( &env );
    rc = SQLAllocConnect( env, &dbc );
    if( SQL_SUCCEEDED(rc) )
    {
        std::cout << "allocation successful" << std::endl;
    }
    std::string _szDSN = "DATABASE=dbname; SERVER=123.45.66.7; PORT=5432; UID=id123; PWD=pw123;";
    rc = SQLDriverConnect( dbc, NULL, (unsigned char*)(_szDSN.c_str()), SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT );
    if( SQL_SUCCEEDED(rc) )
    {
        std::cout << "Connection successful" << std::endl;
    }
    else
    {
        std::cout << "Connection UNsuccessful" << std::endl;
    }


    //*** Exit program
    std::cout.flush();
    std::getchar();
    return 1;
}

0 个答案:

没有答案
相关问题