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