Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I can't connect to my MySQL database [Solved]



  • Hi, I'm learning to program in Qt, but I can't connect with my database.

    I don't find my error, Qt Creator says the variable type is incomplete.
    !http://i46.tinypic.com/a4su2q.png(Error in MySQL connection)!



  • Have you included sql at your pro file? Please also make sure that your Qt MySQL driver is installed. To install it on fedora you should run something like:
    @yum install qt-mysql@



  • Did you include?

    @
    #include <QSqlDatabase>
    @



  • In theory this is enough:

    @
    #include <QtSql>
    ...
    QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL" );
    db.setHostName( "localhost" );
    db.setDatabaseName( "yadayada" );
    db.setUserName( "yadayada" );
    db.setPassword( "yadayada" );

    if( !db.open() )
    {
    qDebug() << db.lastError();
    qFatal( "Failed to connect." );
    }

    qDebug( "Connected!" );
    ...
    @

    In *.pro file:
    @
    QT += sql
    @

    Anyway your error message is a typical text when you forget some include file, in this case:
    #include <QSqlDatabase>



  • Thanks to everyone, I can run with the database including this code in the header of main.cpp.

    @#include <QtSql/QSqlDatabase>@



  • Jus try including Qt += sql in the .pro file and try...


Log in to reply