Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Database connexion Mysql from Qt5.01[solved]

    General and Desktop
    3
    5
    3815
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Marco_105 last edited by

      @#include <QCoreApplication>
      #include <QtSql/QSqlDatabase>
      #include <QtSql>
      #include <QDebug>
      #include <iostream>

      //remplacer l'affichage par la commande cout
      #define q2c(string) string.toStdString()

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      //connection sur la database
      

      // QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      // db.setDatabaseName("myDBLite.db");

      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setHostName("localhost");
      

      // db.setPort(3306);
      db.setUserName("root");
      db.setPassword("");
      db.setDatabaseName("myDataBase");

      if(db.open())
      {
          qDebug() << "Vous etes connecte a " << db.hostName();
      }else{
          qDebug() << "Connexion FAILED.";
          qDebug() << "Driver disponible: " << db.drivers();
          qDebug() << "Driver fonctionnel(QMYSQL): " << db.isDriverAvailable("QMYSQL");
          qDebug() << "numero de l'erreur: " << db.lastError().number();
          qDebug() << "database erreur: " << db.lastError().databaseText();
          qDebug() << "driver erreur: " << db.lastError().driverText();
          qDebug() << "type erreur: " << db.lastError().type();
      }
      
      
      return a.exec&#40;&#41;;
      

      }
      @

      I have compliled mySql driver witch are : qsqlmysql.dll / qsqlmysqld.dll and libqsqlmysqld.a / libqsqlmysql.a

      My SQLServer is include in WampServer on Windows 7,
      so i woul'd like to know what is needed to complete the process and getting connexion well done !

      The above test return :

      "Connexion FAILED."

      Driver available: "QSQLLITE", "QMYSQL", "QMYSQL3"
      Driver IsAvailable(QMYSQL): true
      error number: 2004
      database error: "Unknown MySQL server host 'localhost' (0)"
      driver error: "QMYSQL: Unable to connect"
      type error: 1

      Can you inform what is wrong ? I already look in the doc but don't how to solve it.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Are you sure your server is running ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          sidewinder last edited by

          Does it make any difference if you use 127.0.0.1 instead of localhost?

          "Never memorize what you can look up in books."
          Albert Einstein

          1 Reply Last reply Reply Quote 0
          • M
            Marco_105 last edited by

            Finally the source of the problem was just because i make composant from extern folder with
            Qt5.01 everywhere_qtbase and i use Qt5.01(WingW_msi) for running apps.

            So i was confuse about where exactly put composant and solve it like that:

            libmysql.dll >C:\Qt\Qt5.0.1\5.0.1\mingw47_32\bin

            libqsqlmysql.a, libqslmysql.dll, libqsqlmysqld.a, libqslmysqld.dll

            C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers

            run wamp
            run appli

            result:
            *"Vous etes connecte a 'localhost' " *

            nice, thanks for watching.

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Great you found out !

              Don't forget to update the thread's title to solve so other forum users may know that a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • First post
                Last post