How to connect Qt with postgreSQL?
-
wrote on 10 Dec 2015, 06:02 last edited by
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.. -
wrote on 10 Dec 2015, 07:52 last edited by
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.
-
wrote on 10 Dec 2015, 08:26 last edited by vIJI 12 Oct 2015, 08:28
Thank you for replying, I am sorry, i am the beginner to Qt so will you please help me with the steps.
Thank you.
-
wrote on 10 Dec 2015, 08:38 last edited by
@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....
-
You should read the output more carefully:
bash: cd: /src/plugins/sqldrivers/psql: No such file or directoryThat means there is no /src/plugins/sqldrivers/psql directory.
QTDIR is not set. Just use the whole path to Qt sources directly. -
@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....
wrote on 10 Dec 2015, 08:55 last edited by@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).
-
wrote on 10 Dec 2015, 09:09 last edited by vIJI 12 Oct 2015, 09:24
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.
-
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.
-
wrote on 10 Dec 2015, 15:50 last edited by sirop 12 Oct 2015, 15:52
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.
-
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. -
wrote on 21 Feb 2020, 06:38 last edited by
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.