Database connexion Mysql from Qt5.01[solved]



  • @#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.


  • Lifetime Qt Champion

    Hi,

    Are you sure your server is running ?



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



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


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

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