[SOLVED]Database connexion in a GUI apps with Qt5 (Mingw) seems impossible ?



  • Hello,

    My driver's compilation looks fine (MySQL) and the dispatching is:

    Qt/Qt5.0.1/5.0.1/mingw47_32/bin >libmysql.dll
    Qt/Qt5.0.1/5.0.1/mingw47_32/lib >libqsqlmysql.a et libqsqlmysqld.a
    Qt/Qt5.0.1/5.0.1/mingw47_32/lib/plugins/sqldrivers >qsqlmysql.dll et qsqlmysqld.dll

    That's works fine in a Console app only !...

    but when i try with a GUI apps and following code:

    @#include "dialog.h"
    #include <QApplication>

    bool createConnection()
    {
    QString myDriver = "QMYSQL"; //"QODBC"

    QSqlDatabase db = QSqlDatabase::addDatabase(myDriver);
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("");
    db.setDatabaseName("test"); //"ODBC_DBTest"
    
    if(!db.open())
    {
        qDebug() << "Connexion FAILED.";
        qDebug() << "Driver disponible: " << db.drivers();
        qDebug() << "Driver fonctionnel" << myDriver << ":" << db.isDriverAvailable(myDriver);
        qDebug() << "numero de l'erreur: " << db.lastError().number();
        qDebug() << "database erreur: " << db.lastError().databaseText();
        qDebug() << "driver erreur: " << db.lastError().driverText();
        return false;
    
    }else{
        qDebug() << "Your are now connect to " << db.hostName();
        return true;
    }
    

    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    if(!createConnection())
    return 1;

    Dialog w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    Connexion FAILED.
    Driver disponible: ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3")
    Driver fonctionnel "QMYSQL" : true
    numero de l'erreur: 2005
    database erreur: "Unknown MySQL server host 'localhost' (0)"
    driver erreur: "QMYSQL: Unable to connect"

    What did i miss or how can i find a hack for it ?


  • Moderators

    the error message just says that i couldn't find a mysql server on "localhost"
    Are you sure that you have a mysql server running on your computer?!


  • Moderators

    alternatively you can try to use "127.0.0.1" .
    And make sure that C:\Windows\System32\drivers\etc\hosts has the following entry in it:
    @127.0.0.1 localhost@



  • I have wamp server with mysql, maybe there is a special config with it ?

    Don't know exactly, why that works in a console apps ?



  • Yes, i know that host file is Ok and i can also have connexion by cmd.exe command...



  • Hello World...

    Seems nobody can help on that way ?

    Why connexion on database works on a console apps and will not on a gui apps

    Because security is active and break this behaviour...

    Responsible: Firewall


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.