Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Database connexion Mysql from Qt5.01[solved]

Database connexion Mysql from Qt5.01[solved]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.0k Views
  • 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 Offline
    M Offline
    Marco_105
    wrote on last edited by
    #1

    @#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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        sidewinder
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          Marco_105
          wrote on last edited by
          #4

          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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved