Cannot connect to mysql database
Hi all,
I am writing a little program that open a window with a form. When the user fills the form and click Submit, the app should connect to mysql server and open another window FenP.
But when I click on Submit, I always have the dialog Box saying that the connection failed.
Can you help me?
Below is my code:
main.cpp
Code:
#include <QApplication>
#include "FenetreAccueil.h"
int main(int argc,char* argv[])
{
FenetreAccueil login;
login.show();
return app.exec();
}
FenetreAcceuil.h
Code:
#ifndef FENETREACCUEIL_H
#define FENETREACCUEIL_H
#include <QtGui>
#include <QtSql>
#include "FenetrePrincipale.h"
class FenetreAccueil
: public QWidget{
Q_OBJECT
public:
explicit FenetreAccueil
(QWidget *parent
= 0);
public slots:
void connectDB();
private:
};
#endif // FENETREACCUEIL_H
Code:
#include "FenetreAccueil.h"
FenetreAccueil
::FenetreAccueil(QWidget *parent
) :{
//Creation du layout de formulaire et ses widgets
m_password
->setEchoMode
(QLineEdit::Password);
//affichage d'asterisques a la saisie m_addressBD
=new QLineEdit;
//format adresse IP ou pas?
QFormLayout *formAccueil=new QFormLayout;
formAccueil->addRow(tr("&Login"),m_login);
formAccueil->addRow(tr("Password"),m_password);
formAccueil->addRow(tr("mySQL server address"),m_addressBD);
//Creation d'un layout horizontal pour les boutons
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);
//Creation d'un layout pour le logo
logo
->setPixmap
(QPixmap("images/grandlogo.png"));
logoLayout->addWidget(logo);
//Creation d'un layout pour le message d'acceuil
m_label
= new QLabel(tr
("Welcome in callTracker"),
this);
labelLayout->addWidget(m_label);
//Creation du layout principal de la fenetre
//Ajout du layout d'accueil
layoutPrincipal->addLayout(labelLayout);
//Ajout du layout de formulaire
layoutPrincipal->addLayout(formAccueil);
//Ajout du layout des boutons
layoutPrincipal->addLayout(boutonLayout);
setLayout(layoutPrincipal);
setWindowTitle(tr("Welcome"));
setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
resize(300,350);
//Generation des signaux et des slots
connect(m_connect,SIGNAL(clicked()),this,SLOT(connectDB()));
connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
}
void FenetreAccueil::connectDB()
{
server=m_addressBD->text();
db.setHostName(server);
db.setDatabaseName("calltracker");
db.setUserName("appClient");
db.setPassword("Clown2neige");
bool ok=db.open();
if(ok)
{
FenetrePrincipale *fenP=new FenetrePrincipale;
fenP->show();
}
else
QMessageBox::critical(this,
"callTracker",tr
("Connection to database failed"));
}
Thank you
Re: Cannot connect to mysql database
I don't know if your problem is this, but I had a similar problem. At least on windows, qt framework comes without mysql driver. I think it comes with sqlite and maybe odbc, so maybe you could connect using qodbc or something like that. By the way, you can print the loaded drivers with this line:
qDebug() << QSqlDatabase::drivers();
Re: Cannot connect to mysql database
QSqlDatabase::lastError() and the console output from the program will probably tell you exactly what the problem is.
Fearu is correct about the absence of a Qt MySQL plugin in the shipped Qt SDK on Windows. Most Linux builds will have it, but I don't know if the shipped SDK bundle does.
Re: Cannot connect to mysql database
You're right.
Here is the software output when I try to connect to database.
Démarrage de C:\Users\HP\Desktop\lastClient\callTracker-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\debug\callTracker.exe... QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE
Thanks