1

I am trying to connect to a remote postgresql database from qt using libpq.lib. I have a postgresql installation on my machine and I have installed the ODBC drivers. I use the database with java/jdbc. In my Qt work All I can seem to do is get my application to crash unexpectedly and I can't seem to make any progress.

My connection code is

PGconn * connection;
char       conninfo[250];

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", "user", "password", "foo", "192.168.3.3", 5433);
qDebug() << "Foo1: " << conninfo;

connection = PQconnectdb( conninfo );
qDebug() << "Foo1: ";

Any guidance will be appreciated.

1
  • Use libpq directly or use psqlODBC via the Windows ODBC APIs. You do not need both. Commented Apr 8, 2015 at 2:39

2 Answers 2

2

Definitively you should try Qt's awesome classes:

#include <QSqlDatabase>
#include <QSqlQuery>

Working with Postgres is a piece of cake:

void testConnect()
{
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("mydatabase");
    db.setUserName("myusername");
    db.setPassword("mypassword");
    db.setPort(5432);
    if (db.open())
        qDebug() << "Connection ok!";
    else
        qDebug() << "Connection Failed!";
}

void testQuery()
{
    QSqlQuery query(
        "select table_name "
        "from information_schema.tables "
        "where table_schema = 'public' "
        "order by 1");
     while (query.next()) {
         qDebug() << query.value("table_name").toString();
     }
}

You do not need any ODBC drivers. Read more in a great Qt help system. Remember to add

QT += sql

to your project.pro file.

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is the qt sql driver is very limited. One feature I need is to be able to store arrays in the database and the qt sql driver doesn't support that feature. I had a qt implementation running until i found it didn't support postgresql arrays.
QSql is a crippled toy, it makes easy things easy and anything nontrivial impossible. They should've ripped off the Java API for it, like they do most things, as providing a C++ implementation that looks like JDBC would've been nearly ideal. Unfortunately they didn't.
I thought OP is a Qt novice, my mistake. However, if I have a working sql interface I can do nearly anything Postgres allows, like in a psql shell. It's a matter of proper building queries and interpreting results. Of course, native Postgres interface can make some tasks easier and/or more efficient.
@klin My biggest problem with QSql looks to have been remedied, actually; it now (finally) supports parameter binding, which is the minimum required for any real world SQL language binding IMO. It still seems to lack query pipelining, but then so does libpq, you only get that with PgJDBC.
2

Use libpq directly or use psqlODBC via the Windows ODBC APIs. You do not need both. You don't seem to be using psqlODBC at all, so we can ignore that, it doesn't matter that it's installed.

Your code looks OK, though you really should be using snprintf to prevent overrunning the size of your statically allocated conninfo buffer and introducing a stack overwrite exploit via buffer overflow into your code.

You should also check for connection errors:

if (PQstatus(connection) != CONNECTION_OK)
{
  qDebug() << "Connection failed: " << PQerrorMessage(connection);
  exit(1);
}

I suspect that you are having issues related to multiple, probably incompatible, versions of libpq. You are probably compiling against the libpq.lib for one libpq, but then at runtime linking to another libpq. This is especially likely with Qt, which provides its own SQL interface classes that may link to libpq.

To diagnose what's happening will probably require use of a debugger. Since you're on Windows, that means Visual Studio, because windbg.exe is only for masochists. If you do not have Visual Studio, get the free Express version here. You need "Express 2013 for Windows Desktop". (Microsoft changes the names in new and confusing ways with each release). Run your program under Visual Studio's debugger, and when it crashes, examine how/why.


As @kiln points out there's also QSql ... but it's very limited and IMO unsuitable for production applications where database access is a significant priority. (Edit: It looks like it's a lot better in Qt 5.4; I hadn't used it much since Qt4, and they've finally fixed the biggest omissions that made QtSQL practically unusable in the real world now, like the lack of access to the native SQLSTATE from the DB, lack of parameter binding, etc).

7 Comments

Does the postgresql service need to be running?
@Sting Well, you need a database to connect to, sure. But you'll get an error (see edit above) if connection fails.
I didn't ask the question correctly :(.. I have a postgres server on the local machine and I figure I don't need it runnin to connect to the remote machine. I successfully connected from Eclipse so I know the connection is possible. If I could find an easy way to write a little UI in eclipse I could make progress. Thanks for your help.
@Sting Correct, if you're connecting to a remote machine, you do not need PostgreSQL running locally. Really, run your program under a debugger. Also, as per edits above, I retract my prior comments about QtSQL; it looks like the main issues with it have been fixed since I last used it, and it might be suitable now.
I need to be able to insert arrays into the remote database. I am building a data acquisition application and I have to process lots of data. The Qt sql does not support arrays.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.