QT PostgreSQL connection
-
wrote on 20 Jun 2014, 12:07 last edited by
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...
-
wrote on 20 Jun 2014, 12:38 last edited by
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.
-
wrote on 20 Jun 2014, 18:49 last edited by
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
-
wrote on 21 Jun 2014, 13:52 last edited by
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.
-
wrote on 23 Jun 2014, 11:14 last edited by
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.
-
wrote on 23 Jun 2014, 12:58 last edited by
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 -
wrote on 5 Jul 2014, 17:48 last edited by
->stoyanps
there is no video for me on this siteNew 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 Qt5Everything 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.
-
wrote on 7 Jul 2014, 17:51 last edited by
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.