rickym
24th March 2007, 15:00
Hello everybody!
I have written a program that needs to connect to a mysql db installed on one of the computers in the network. All others computers in the network running the program should be able to connect to the db.
I have a script that connect to the server, it looks like this:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QFile>
#include <QTextStream>
#include <QString>
static bool createConnection()
{
QFile file;
file.setFileName("connection.txt");
if (!file.open(QFile::ReadOnly| QFile::Text)) {
QMessageBox::critical(0, qApp->tr("Error"),
qApp->tr("Cannot open connection file."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QTextStream in(&file);
QString DatabaseName = in.readLine();
QString UserName = in.readLine();
QString Password = in.readLine();
QString HostName = in.readLine();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
db.setPort (3306);
db.setDatabaseName( DatabaseName );
db.setUserName( UserName );
db.setPassword( Password );
db.setHostName( HostName );
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}
#endif
The program works when running on the computer where MySQL is installed but fails to connect when running on all the computers in the network.
I am using the ip address of the MySQL server as hostname.
Hope that someone could help me out in this.
Thanks in advance!
I have written a program that needs to connect to a mysql db installed on one of the computers in the network. All others computers in the network running the program should be able to connect to the db.
I have a script that connect to the server, it looks like this:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QFile>
#include <QTextStream>
#include <QString>
static bool createConnection()
{
QFile file;
file.setFileName("connection.txt");
if (!file.open(QFile::ReadOnly| QFile::Text)) {
QMessageBox::critical(0, qApp->tr("Error"),
qApp->tr("Cannot open connection file."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QTextStream in(&file);
QString DatabaseName = in.readLine();
QString UserName = in.readLine();
QString Password = in.readLine();
QString HostName = in.readLine();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
db.setPort (3306);
db.setDatabaseName( DatabaseName );
db.setUserName( UserName );
db.setPassword( Password );
db.setHostName( HostName );
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection."
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}
#endif
The program works when running on the computer where MySQL is installed but fails to connect when running on all the computers in the network.
I am using the ip address of the MySQL server as hostname.
Hope that someone could help me out in this.
Thanks in advance!