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. QT PostgreSQL connection
QtWS25 Last Chance

QT PostgreSQL connection

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.2k 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.
  • N Offline
    N Offline
    NewToIt
    wrote on last edited by
    #1

    Hello,

    up front I would like to say that I am new to QT 3.1.1 OpenSource and C++. For 2 days I have been searching for a solution and would appreciate any help. I would like to establish a PostgreSQL connection with my application on Win8/64bit. My first program is running, but I keep getting an error trying to connect to PostgreSQL. I would like to establish a connection and later on run SQL queries with my code.

    @QSqlDatabase: QPSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
    Error@

    My code looks like this
    @// Within .pro
    QT += sql

    // Within .cpp
    #include <QSqlDatabase>
    #include <QtSql>

    void MainWindow::on_pushButton_RunImport_clicked()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("MyDaBase");
    db.setUserName("postgres");
    db.setPassword("dbpass");

    if(!db.open()) 
        ui->label->setText("Connection Failed!"); 
    else 
        ui->label->setText("Connected"); 
    

    }@

    I know QT is probably missing a driver or a path to find driver for PostgreSQL due to license issues. What files are missing and where do they belong? Where do I get those files? Or am I missing some code? I tried to find a solution with QT Wiki/Doc, but for a beginner it's hard.

    Thank you...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stoyanps
      wrote on last edited by
      #2

      This files must be in the folder of your application or in PATH:
      @libpq.dll
      libeay32.dll
      ssleay32.dll
      libintl.dll@

      They are in <PostgreSQL folder>\bin

      Also in subfolder "sqldrivers" you have to add this file: "qsqlpsql.dll".
      You can find it in "<QTDIR>\plugins\sqldrivers".

      Make sure that both version of Qt and PostgreSQL are same type (64-bit or 32-bit). I build an application with Qt 5 (Mingw-32) and it didn't work with 64-bit version of PostgreSQL. This can be my fault, but still consider this case.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        clochydd
        wrote on last edited by
        #3

        Hi, and welcome to DevNet,

        to start using PostgreSQL with Qt you have to build the QPSQL driver, see "here (QPSQL for PostgreSQL): ":http://qt-project.org/doc/qt-5/sql-driver.html#qpsql

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NewToIt
          wrote on last edited by
          #4

          Dear stoyanps,

          thank you for providing a very easy to follow instruction. I copy paste the following files from PostgreSQL to the folder where my QT application (I am currently working on) is located:

          @libpq.dll
          libeay32.dll
          ssleay32.dll
          libintl-8.dll@

          I am running Win8/64bit and I also installed 64bit QT version. However, PostgreSQL is a 32bit version I believe. It works with a 32bit external program. The mentioned "qsqlpsql.dll" is already present within "<QTDIR>\plugins\sqldrivers"

          I am still getting the same error. Do I have to install a 32bit QT version or is there a way to work around this issue?

          My goal for the application is to use as much RAM as possbile, therefore 64bit.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stoyanps
            wrote on last edited by
            #5

            As far as I know, Qt Mingw (which I use) has only 32-bit version pre-build. If you have 64-bit version and didn't build it yourself, then you probably have version VS 2013. I didn't use it, but I think you can compile with it your code to 32-bit application and see if it work.

            You can also use Dependency Walker on qsqlpsql.dll to check what DLLs are needed. For VS 2013 they may be different.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stoyanps
              wrote on last edited by
              #6

              I just find video instructions how to use plugin of PostgreSQL and how to make it yourself:
              "Qt 5.3 MinGW + PostgreSQL 9. Make SQL driver and database connectiion on Windows 7":
              "www.youtube.com/watch?v=fBgJ9Azm_S0":http://www.youtube.com/watch?v=fBgJ9Azm_S0

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NewToIt
                wrote on last edited by
                #7

                ->stoyanps
                there is no video for me on this site

                New setup...Now I have Visual Studio 2013 installed. I downloaded, installed and ran the following Qt files on Windows 8.1 (64bit):

                a) Qt 5.3.1 for Windows 32-bit (VS 2013, 559 MB)
                b) Visual Studio Add-in 1.2.3 for Qt5

                Everything went fine and I configured the correct qmake.exe path with Visual Studio 2013->Qt5->Options which is C:\Qt\Qt5.3.1\5.3\msvc2013. Therefore Qt does run within Visual Studio 2013 now.

                I am still trying to connect to postgreSQL 9.0 (32bit) with my Qt application and I am still facing the error:

                @QSqlDatabase: QPSQL driver not loaded
                QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7@

                I now there are files missing, but I have no idea how to solve this. I found an old thread here:

                Solution: http://qt-project.org/forums/viewthread/20397

                in the executable’s path make sure you have the following files (I suppose you’re using windows):

                @comerr32.dll
                gssapi32.dll
                k5sprt32.dll
                krb5_32.dll
                libgcc_s_dw2-1.dll
                libiconv-2.dll
                libintl-8.dll
                libpq.dll
                mingwm10.dll
                msvcr71.dll
                you may also need:
                libssl32.dll
                libeay32.dll
                ssleay32.dll@

                Where do I get all those files? Where do I have to move them?

                I also tried to use Dependency Walker, but found no solution there either since I don't know how to handle it.

                Since I am new to C++, Visual Studio and Qt it would be very kind if someone would help with a step by step solution on how to solve this.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  stoyanps
                  wrote on last edited by
                  #8

                  Sorry, I fixed the link. Look at it!
                  Did you try with the list of files which I posted before?
                  They are the only files related to PostgreSQL and must be placed in the folder where your program is.
                  You also need other files for deploying your application, but there is another theme on this site about that.

                  1 Reply Last reply
                  0

                  • Login

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