PDA

View Full Version : connection QT with postgresql



khadija123
22nd March 2012, 20:01
:)hi,
i would like you to help me with the problem of the connection between qt and database postgresql :(
i download QT 4.7.3
and postgresql 9.1.1
windows 7
and i downloaded the connector odbc postgresql to create a database source in order to connect qt with postgresql, this is the code that i wrote but i had a message error
driver not loaded
#include <QtGui/QApplication>
#include "mainwindow.h"
#include<QtSql>
#include<QtDebug>
#include<Iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("localhost");
db.setDatabaseName("dsi64");
db.setPassword("123");
db.setUserName("postgres");
if(db.open())
{
qDebug() <<"opened" ;
db.close();
}
else
{
qDebug() << db.lastError().text();
}
MainWindow w;
w.show();

return a.exec();
}
please can you help me

Lykurg
22nd March 2012, 21:16
QSQLITE is surely not the driver you need to connect to a postgresql database! It is for SQLite databases...

khadija123
22nd March 2012, 21:25
sorry for the error ,this is the right code that i use ,
#include <QtGui/QApplication>
#include "mainwindow.h"
#include<QtSql>
#include<QtDebug>
#include<Iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("dsi64");
db.setPassword("khadija");
db.setUserName("postgres");
if(db.open())
{
qDebug() <<"opened" ;
db.close();

}
else
{
qDebug() << db.lastError().text();
}
MainWindow w;
w.show();


return a.exec();
}

and i get the error driver not loaded qsqlite is available and when i use it i get opened

Lykurg
23rd March 2012, 06:30
Well, you don't get it opened! You get a file created called dsi64 at your current directory. As the error says, you haven't the driver, so build it. And how about using QPSQL?

On how to build driver see http://qt-project.org/doc/qt-4.8/sql-driver.html

khadija123
24th March 2012, 11:24
well, i think the problem is with qt because when i use the same driver postgresql with java i have no problem with the connection with the database .
i don't know if it is qt installation or the version

ChrisW67
24th March 2012, 23:44
The problem is not with Qt. The problem is with your understanding.

The Qt plugin for direct access to PostgreSQL is called PSQL not QSQLITE or QODBC, just as Lykurg suggested. You should prefer the PSQL driver to access PostgreSQL database directly from Qt programs. You can use the ODBC interface to PostgreSQL if there is an ODBC driver for PostgreSQL on your machine and an ODBC data source named dsi64 with the specified credentials.

However, since you have built neither the Qt ODBC plugin nor the Qt PostgreSQL plugin your efforts to connect to a PostreSQL database through either plugin will not get you too far, just "Driver not loaded". Please have read about the supported database types in particular the part about building the plugin for PostgreSQL.

khadija123
31st March 2012, 21:09
thank you for your reply i found a solution i installed another version QT SDK 2010.05 and it works but i still have some problems such as
for exemple
the class QSqlQuery he says that bindValue is not a function in this class and i don't know what is the problem
can you help me with this
and thanks again