PDA

View Full Version : network application



khadija123
7th April 2012, 12:27
what I have to do is to put the database on one machine and access to it from different machines on which the application is installed
what we must do is
QSqlDatabase db = QSqlDatabase :: addDatabase (......)
we must provide the parameters to the function addDatabase such as machine name, port, and the driver
can you explain me these parameters and provide me with the arguments of the function
thanks

Spitfire
9th April 2012, 14:42
QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL", "MyUniqueConnectionName" ); // first argument is driver name, second is unique connection name
db.setDatabaseName( "MyDatabaseName" ); // name of your database on the server
db.setHostName( "my_host_name.co.uk" ); // hostname of your database server
db.setPort( 3306 ); // port of the sql server
db.setUserName( "db_user_name" ); // user name in the database
db.setPassword( "your_super_secret_password" ); // password for that user

if( db.open() )
{
// connected
}
else
{
// not connected
}


In fact everythign is explained in the docummentation (http://doc.qt.nokia.com/4.7/qsqldatabase.html), did you read it?

khadija123
9th April 2012, 18:19
thanks for your reply,
i already did all of this, i had a problem just in the first instruction addDatabase
that's why i asked

ChrisW67
10th April 2012, 00:35
So you already did what Spitfire has suggested, i.e. read the documentation and written code like the example above. Clearly, the QSqlDatabase::addDatabase() function does not take machine name, port, user name or password as an argument, yet that is what you asked. What exactly is the problem?

khadija123
10th April 2012, 10:52
my professor told me that if i want to make the application on network i have to add other parameters as name of the machine and the bridhe
so i can put the database in another machine and access to it in any other machine where the application is already installed
thanks again

that's my program

#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("localhost");
db.setDatabaseName("dsi64");
db.setPassword("khadija");
db.setUserName("postgres");
db.setPort(5432);
if(db.open())
{
qDebug() <<"opened" ;
QSqlQuery query;
query.exec("SELECT id_niveau,intitule from niveau;");

while (query.next())
{
QString intitule = query.value(1).toString();
int id_niveau = query.value(0).toInt();
qDebug() << qPrintable(intitule) << ": " << id_niveau << endl;
}
db.close();
}
else
{
qDebug() << db.lastError().text();
}
MainWindow w;
w.show();

return a.exec();
}

Spitfire
10th April 2012, 11:05
So, what's the problem?

You can't connect to remote host?
No wonder there as you're using 'localhost' as hostname.
use setHostName() to set target machine host and that's it.

Did you read the doc (or at least the comments in my example)?

btw machine name and hostname are two different things, don't confuse them.

khadija123
10th April 2012, 11:14
when i put the database in the other machine , can i put in datasource odbc another server and not localhost :just another name so that if i put that name in the fonction setHostName it would work
thanks for your attention

Lesiok
10th April 2012, 11:27
Why are You trying to connect to PostgreSQL with ODBC driver not with PGSQL ?

khadija123
10th April 2012, 12:18
i'm used to work in odbc and it works well
i dont know what i'm going to put in server in odbc it accepts just localhost