PDA

View Full Version : postgresql connection network



khadija123
11th April 2012, 14:14
hi everyone,
i want to make a connection between a qt program installed in a client machine and a postgresql database stored on a server
this is the program i wrote

#include <QtGui/QApplication>
#include<QtSql>
#include<QtDebug>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db =QSqlDatabase::addDatabase("QODBC");

db.setHostName("10.9.173.168"); //this ip adress is the adress of the server machine and i used it in odbc and i have connection succeful
db.setDatabaseName("khaw");
db.setPassword("khawla");
db.setUserName("postgres");
db.setPort(5432);

if(db.open())
{
qDebug() <<"opened" ;

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

return a.exec();
}
but i still have the error "[Microsoft][Gestionnaire de pilotes ODBC] Source de données introuvable et nom de pilote non spécifié QODBC3: Unable to connect"
which is in english [Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified
in odbc i wrote this:
data source:khaw
database:basee
server:10.9.173.168
user name:posgres
ssl mode:disable
port:5432
password:khawla
can you tell me what i should do to connect
thanks for your reply

Spitfire
12th April 2012, 12:33
You did connect.
Error message you're getting comes from server.

Besides, I think you're setting database name wrong, try 'basee' instead of 'khaw'.

If you google for the error, many people say it happens when you don't use dns to connect to server.
Maybe that's it?

khadija123
12th April 2012, 15:07
i didn't really understand what's the error that you mean
can you explain more
thanks for your reply

Spitfire
12th April 2012, 15:56
[Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified
Google for it.
There's quite few people having this issue.

Using IP instead of hostname seems to be one of the reasons for getting this error.

Did you check if using 'basee' as the database name changes anything? From what you've posted above seems that it may be relevant.

khadija123
12th April 2012, 16:49
in setdatabasename we put the odbc data source not the name of database
and if i put localhost it's just my computer and not in network

Spitfire
12th April 2012, 17:13
From the documentation:


For the QODBC driver, the name can either be a DSN, a DSN filename (in which case the file must have a .dsn extension), or a connection string.

For example, Microsoft Access users can use the following connection string to open an .mdb file directly, instead of having to create a DSN entry in the ODBC manager:

...
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
if (db.open()) {
// success!
}
...