Results 1 to 4 of 4

Thread: Cannot connect to mysql database

  1. #1
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include <QApplication>
    2. #include "FenetreAccueil.h"
    3.  
    4. int main(int argc,char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7. FenetreAccueil login;
    8. login.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    FenetreAcceuil.h

    Qt Code:
    1. #ifndef FENETREACCUEIL_H
    2. #define FENETREACCUEIL_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6. #include "FenetrePrincipale.h"
    7.  
    8.  
    9. class FenetreAccueil : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit FenetreAccueil(QWidget *parent = 0);
    15.  
    16. public slots:
    17. void connectDB();
    18.  
    19. private:
    20. QPushButton *m_connect;
    21. QPushButton *m_cancel;
    22. QLineEdit *m_password;
    23. QLineEdit *m_login;
    24. QLineEdit *m_addressBD;
    25. QLabel *m_label;
    26.  
    27. };
    28.  
    29. #endif // FENETREACCUEIL_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "FenetreAccueil.h"
    2.  
    3. FenetreAccueil::FenetreAccueil(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. //Creation du layout de formulaire et ses widgets
    7. m_login=new QLineEdit;
    8. m_password=new QLineEdit;
    9. m_password->setEchoMode(QLineEdit::Password);//affichage d'asterisques a la saisie
    10. m_addressBD=new QLineEdit;//format adresse IP ou pas?
    11.  
    12. QFormLayout *formAccueil=new QFormLayout;
    13. formAccueil->addRow(tr("&Login"),m_login);
    14. formAccueil->addRow(tr("Password"),m_password);
    15. formAccueil->addRow(tr("mySQL server address"),m_addressBD);
    16.  
    17. //Creation d'un layout horizontal pour les boutons
    18. m_connect=new QPushButton(tr("Connect"));
    19. m_cancel=new QPushButton(tr("Cancel"));
    20.  
    21. QHBoxLayout *boutonLayout=new QHBoxLayout;
    22. boutonLayout->addWidget(m_connect);
    23. boutonLayout->addWidget(m_cancel);
    24.  
    25.  
    26. //Creation d'un layout pour le logo
    27. QLabel *logo=new QLabel(this);
    28. logo->setPixmap(QPixmap("images/grandlogo.png"));
    29.  
    30. QHBoxLayout *logoLayout=new QHBoxLayout;
    31. logoLayout->addWidget(logo);
    32.  
    33.  
    34. //Creation d'un layout pour le message d'acceuil
    35. m_label = new QLabel(tr("Welcome in callTracker"),this);
    36.  
    37. QHBoxLayout *labelLayout=new QHBoxLayout;
    38. labelLayout->addWidget(m_label);
    39.  
    40. //Creation du layout principal de la fenetre
    41. QVBoxLayout *layoutPrincipal=new QVBoxLayout;
    42.  
    43. //Ajout du layout d'accueil
    44. layoutPrincipal->addLayout(labelLayout);
    45.  
    46. //Ajout du layout de formulaire
    47. layoutPrincipal->addLayout(formAccueil);
    48.  
    49. //Ajout du layout des boutons
    50. layoutPrincipal->addLayout(boutonLayout);
    51.  
    52. setLayout(layoutPrincipal);
    53. setWindowTitle(tr("Welcome"));
    54. setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
    55. resize(300,350);
    56.  
    57. //Generation des signaux et des slots
    58. connect(m_connect,SIGNAL(clicked()),this,SLOT(connectDB()));
    59. connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
    60. }
    61.  
    62. void FenetreAccueil::connectDB()
    63. {
    64. QString server;
    65. server=m_addressBD->text();
    66.  
    67. QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    68. db.setHostName(server);
    69. db.setDatabaseName("calltracker");
    70. db.setUserName("appClient");
    71. db.setPassword("Clown2neige");
    72. bool ok=db.open();
    73.  
    74. if(ok)
    75. {
    76. FenetrePrincipale *fenP=new FenetrePrincipale;
    77. fenP->show();
    78. }
    79. else
    80. QMessageBox::critical(this,"callTracker",tr("Connection to database failed"));
    81.  
    82. }
    To copy to clipboard, switch view to plain text mode 

    Thank you

  2. The following 2 users say thank you to phapha for this useful post:


  3. #2
    Join Date
    Aug 2010
    Posts
    15
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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();

  4. The following 2 users say thank you to fearu for this useful post:


  5. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  6. The following 2 users say thank you to ChrisW67 for this useful post:


  7. #4
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  8. The following 2 users say thank you to phapha for this useful post:


Similar Threads

  1. Connect to database
    By seink in forum Newbie
    Replies: 12
    Last Post: 26th April 2011, 18:43
  2. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 00:52
  3. Mysql unknown database, QMYSQL unable to connect
    By lixo1 in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 21:39
  4. Qt connect different MySQL
    By weixj2003ld in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2009, 08:35
  5. How to connect MySQL with QT
    By diego in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 05:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.