Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PostgreSQL connection program crash
Forum Updated to NodeBB v4.3 + New Features

PostgreSQL connection program crash

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sibyx
    wrote on 6 May 2012, 17:20 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sibyx
      wrote on 9 May 2012, 13:14 last edited by
      #2

      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();@

      1 Reply Last reply
      0

      1/2

      6 May 2012, 17:20

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved