Connection to a MySQL database with Qt Creator on Windows



  • Hi everybody.
    I have one problem and I need your help.
    I'm working on a Qt project where I need to connect the software to a database.
    I try this code in my main code source

    bool createConnection()
    {
        QSqlDatabase laBase = QSqlDatabase::addDataBase("QMYSQL");
        laBase.setHostName("localhost");
        laBase.setDataBaseName("test");
        laBase.setUserName("justin");
        laBase.setPassword("******");
    
        if (!laBase.open()){
           QMessageBox:: critical (0 /* ... */ );
           QObject::tr("Error"), 
           laBase.lastError().text());
           return false;
        } 
        return true; 
    }
    

    But when I run it, I have a message box with this characteristics:
    Title of the message box: Error
    Content: Driver not loaded Driver not loaded

    And I have this message error

    QSqlDatabase : QMYSQL driver not loaded
    QSqlDatabase : available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7   
    

    I've a similar error when I use QSQLITE.

    I'm on Windows 10, 64 bits
    With Qt Creator 4.0.2 based on Qt 5.7.0

    I'll be very happy to be helped.

    Thanks.

    [Edit: added code tags ~~ @Wieland]



  • @Justin_Ahinon said in Connection to a MySQL database with Qt Creator on Windows:

    QMYSQL

    Hi
    Did you try with fresh install of Qt?



  • @Advanced
    No.
    But I've uninstalled and reinstalled Qt.

    This night, I'll install the new Qt 5.10 and try again.

    Thanks.



  • It's probably fixed your issue, but i recommend to you if you want to sure about stability used latest LTS version of Qt that it's 5.9.3



  • @Justin_Ahinon
    If this error occurred also with SQLITE, then check if you have in your *.pro file something like this:

    QT += sql
    

  • Lifetime Qt Champion

    Hi,

    To add to my fellow, do you have the MySQL client libraries available on your system ?



  • @SGaist
    Yes, I think


  • Lifetime Qt Champion

    Run your application with the QT_DEBUG_PLUGINS environment variable set to 1 to see what is happening.

    You can set that variable in the Run part of the project panel.



  • I would make sure you complie the mysql plugin..

    1. make sure you have installed mysql the same lib headers for x86 or x64.
    2. open mysql plugin located in qbase you will have to comment out a few things as they still havnt fix the bug yet.
    3. copy those libs and dlls into your complier folder
    4. copy mysqlclient.dll and qt5sql.dll into your .exe folder
    5. should be all set...


  • Hi every body,
    I solve the problem by using the QSQLITE driver which is installed with Qt.

    /.pro file
    QT += widgets sql
    
    //.cpp file
    bool createConnection()
    {
        QSqlDatabase laBase = QSqlDatabase::addDataBase("QSQLITE");
        laBase.setHostName("localhost");
        laBase.setDataBaseName("test");
        laBase.setUserName("justin");
        laBase.setPassword("******");
    
        if (!laBase.open()){
           QMessageBox:: critical (0 /* ... */ );
           QObject::tr("Error"), 
           laBase.lastError().text());
           return false;
        } 
        return true; 
    }
    

  • Lifetime Qt Champion

    Hi,

    Are you aware of the implications of moving from MySQL to SQLite ?

    One of which is: you are using a relative path for your SQLite database which won't work if your users install your application in e.g. Program Files as it's a read-only location.


Log in to reply
 

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