How to connect Qt with postgreSQL?



  • Hi,
    how do i Fix the problem of
    "QSqlDatabase: QPSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE
    "Driver not loaded Driver not loaded"

    its really getting weird for me to solve this please help and save me.

    My code is as follows
    #include "mainwindow.h"
    #include <QApplication>
    #include <QSqlDatabase>
    #include <QDebug>
    #include "QSqlError"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("conf");
    db.setPassword("cdt321");
    db.setUserName("postgres");
    if(db.open())
    {
    qDebug() <<"opened" ;
    db.close();

    }
    else
    {
    qDebug() << db.lastError().text();
    exit(1);
    }
    MainWindow w;
    w.show();
    
    return a.exec();
    

    }
    Thank you in advance..



  • Hi and welcome to devnet,

    you have to check 2 things:

    1. The plugin database driver must be built;
    2. the postgres client library must be in the PATH

    you can find some information here.



  • I think you can avoid the necessity of the Qt plugin database driver by using Postgres libs directly with something like that in your .pro file:

    win32:INCLUDEPATH += "C:/PostgreSQL/9.4/include"
    unix:INCLUDEPATH += /usr/include/postgresql
    
    win32:LIBS += "C:/PostgreSQL/9.4/lib/libpq.lib"
    unix:LIBS += -L/usr/lib -lpq
    

    Of course, your Qt compiler and the compiler, which the Postgres libpq was built with,
    should be the same.

    This is not the level of abstraction you achieve with QPSQL driver but I guess you get less overhead by simply writing a C++ Wrapper for libpq C.



  • Thank you for replying, I am sorry, i am the beginner to Qt so will you please help me with the steps.

    Thank you.



  • @mcosta , i tried running those commands in terminal but the response was..as follows

    root@rcdtcpu24:/# cd $QTDIR/src/plugins/sqldrivers/psql
    bash: cd: /src/plugins/sqldrivers/psql: No such file or directory
    root@rcdtcpu24:/# qmake "INCLUDEPATH+=/usr/include/pgsql" "LIBS+=-L/usr/lib -lpq" psql.pro
    Cannot find file: psql.pro.
    root@rcdtcpu24:/# make
    make: *** No targets specified and no makefile found. Stop.

    Please do help with the steps....


  • Moderators

    You should read the output more carefully:
    bash: cd: /src/plugins/sqldrivers/psql: No such file or directory

    That means there is no /src/plugins/sqldrivers/psql directory.
    QTDIR is not set. Just use the whole path to Qt sources directly.



  • @vIJI
    So you seem to be on Linux?

    If you have an installation of Postgres on your machine, then you have to find out
    where Postgres include and lib directories are.

    The include these paths into your Qt project file (yourproject.pro).



  • Nope its not working......
    i have added them, the location was just "/usr"....

    unix:INCLUDEPATH += /usr/include/
    in .pro file but im unfortunate to get it success..

    can you tell me how to build it step by step please if you dont mind...

    thank you.



  • @sirop The code posted uses Qt to connect to the DB; so he MUST use the plugin.



  • @mcosta

    You are right about the code connecting to the DB.
    But it is not true that you have to use QSql* in order to connect to the DB.
    Postgres has C API for this purpose which one can easily write a C++ wrapper for.

    So my advice was: if Qt way is too abstract, do it with a C++ wrapper of Postgres C API.



  • @sirop, You're right: you don't have to use Qt to connect to the DB but the topic of the thread is How to connect Qt with PostgreSQL so probably the author wants to understand how to do it.


  • Lifetime Qt Champion

    Hi,

    @vIJI First thing to check: How did you install Qt ? Are you using the Online Installer ? Your distribution's package manager ?

    If the former: did you install the Qt sources ? You can make that through the installer.
    If the later, what distribution is it ? Most of them provide all database drivers.


Log in to reply
 

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