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. How to connect Qt with postgreSQL?

How to connect Qt with postgreSQL?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 17.6k Views
  • 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.
  • V Offline
    V Offline
    vIJI
    wrote on 10 Dec 2015, 06:02 last edited by
    #1

    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..

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 10 Dec 2015, 07:12 last edited by
      #2

      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.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sirop
        wrote on 10 Dec 2015, 07:52 last edited by
        #3

        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.

        To be, or not to be: that is the question:
        Whether ’tis nobler in the mind to suffer
        The slings and arrows of outrageous fortune,
        Or to take arms against a sea of troubles,
        And by opposing end them?

        M 1 Reply Last reply 10 Dec 2015, 14:44
        0
        • V Offline
          V Offline
          vIJI
          wrote on 10 Dec 2015, 08:26 last edited by vIJI 12 Oct 2015, 08:28
          #4

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

          Thank you.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vIJI
            wrote on 10 Dec 2015, 08:38 last edited by
            #5

            @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....

            S 1 Reply Last reply 10 Dec 2015, 08:55
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 10 Dec 2015, 08:52 last edited by
              #6

              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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • V vIJI
                10 Dec 2015, 08:38

                @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....

                S Offline
                S Offline
                sirop
                wrote on 10 Dec 2015, 08:55 last edited by
                #7

                @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).

                To be, or not to be: that is the question:
                Whether ’tis nobler in the mind to suffer
                The slings and arrows of outrageous fortune,
                Or to take arms against a sea of troubles,
                And by opposing end them?

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  vIJI
                  wrote on 10 Dec 2015, 09:09 last edited by vIJI 12 Oct 2015, 09:24
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • S sirop
                    10 Dec 2015, 07:52

                    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.

                    M Offline
                    M Offline
                    mcosta
                    wrote on 10 Dec 2015, 14:44 last edited by
                    #9

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

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    S 1 Reply Last reply 10 Dec 2015, 15:50
                    0
                    • M mcosta
                      10 Dec 2015, 14:44

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

                      S Offline
                      S Offline
                      sirop
                      wrote on 10 Dec 2015, 15:50 last edited by sirop 12 Oct 2015, 15:52
                      #10

                      @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.

                      To be, or not to be: that is the question:
                      Whether ’tis nobler in the mind to suffer
                      The slings and arrows of outrageous fortune,
                      Or to take arms against a sea of troubles,
                      And by opposing end them?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on 10 Dec 2015, 20:36 last edited by
                        #11

                        @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.

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 10 Dec 2015, 21:08 last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MuratCan
                            wrote on 21 Feb 2020, 06:38 last edited by
                            #13

                            Hello,
                            First of all, you get this error because the postgresql you have installed is missing. You need to install the postgresql ODBC package. After installing it, you need to introduce the / bin file into the system environment or .pro
                            Good luck.

                            1 Reply Last reply
                            1

                            • Login

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