PostgreSQL connection program crash



  • Hi guys,
    I'm trying to create connection to Postgre SQL database using libpq library. That's my code:
    postgreconn.h"
    @#include "libpq-fe.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <QtCore>
    class PostgreConn
    {
    public:
    PostgreConn(QString host, QString port, QString user, QString password, QString database);
    void close();
    ~PostgreConn();
    private:
    PGconn *conn;
    PGresult *res;
    };@
    postgreconn.cpp:
    @#include "postgreconn.h"
    PostgreConn::PostgreConn(QString host, QString port, QString user, QString password, QString database)
    {
    conn = PQconnectdb(QString("hostaddr = '" + host + "' port = '" + port + "' dbname = '" + database + "' user = '" + user + "' password = '" + password + "' connect_timeout = '10'").toAscii().data());
    if (PQstatus(conn) != CONNECTION_OK) {
    fprintf(stderr, "Connection to database failed: %s",PQerrorMessage(conn));
    }
    else {
    qDebug() << "Connected";
    }
    }

    void PostgreConn::close()
    {
    this->~PostgreConn();
    }

    PostgreConn::~PostgreConn()
    {
    PQfinish(conn);
    }@
    and part of ui constructor:
    @qDebug() << "PostgreSQL Inicialization starts";
    PostgreConn db("192.168.1.30", "5432", "postgres", "pass", "test");
    db.close();@
    Now, when I compiled and executed my program, it crushed and I have output:
    Starting C:\Users\magia\Qt\DatabaseExporter\debug\DatabaseExporter.exe...
    PostgreSQL Inicialization starts
    The program has unexpectedly finished.
    C:\Users\magia\Qt\DatabaseExporter\debug\DatabaseExporter.exe exited with code -1073740940
    I have Windows 7 64bit (if it helps) and I'm using msvc2010 compiler. I don't know where is mistake. Can you help me with it? Thanks for help.



  • Solved, I had bad declaration of object PostgreConn. I fix it:
    @qDebug() << "PostgreSQL Inicialization starts";
    PostgreConn *db;
    db = new PostgreConn("192.168.1.30", "5432", "postgres", "pass", "test");
    db->close();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.