Error while connecting mysql with qt



  • I was trying to connect mysql with qt but got following error :
    Driver not loaded
    My code is as follows:
    widget.cpp

    #include "widget.h"
    #include "ui_widget.h"
    #include <QMessageBox>
    #include <QtSql/qsqldatabase.h>
    #include <QSqlError>
    #include <QtSql>
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    
    bool Widget::conectar()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("charu");
        db.setUserName("root");
        db.setPassword("admin");
    
        if(!db.open())
        {
            QMessageBox::critical(0,"Database Error",db.lastError().text());
            return false;
        }
    
        QMessageBox::warning(this,"Conexion","Conexion Extitosa");
        QSqlQuery query;
        query.exec("SHOW DATABASES");
        QString salida;
        while(query.next())
        {
            salida+=query.value(0).toString()+"\n";
        }
        ui->textEdit->setText(salida);
    
        return  true;
    
    }
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
    public slots:
        bool conectar();
    
    private:
        Ui::Widget *ui;
    };
    
    #endif // WIDGET_H
    

    Can you please help me out to solve the problem?

    [edit: added missing coding tags: 3 ` before and after SGaist]



  • There could be a few reasons. First thing to check is that you have the QMYSQL driver:

    QStringList Items = QSqlDatabase::drivers();
    QStringList::const_iterator StrIter;

    qWarning("Available database drivers:");
    for(StrIter = Items.constBegin();StrIter != Items.constEnd();StrIter++)
    {
    qWarning(QString(" Driver: %1\n").arg(*StrIter).toLatin1());
    }

    If you don't see QMYSQL then you might have the following problems:

    • Don't have the plugin for mysql compiled
    • Don't have the plugin for mysql in a location the application can find

    If you do have the QMYSQL driver the problem could be

    • Plugin is compiled using different compiler and/or not compatible
    • Some other reason that driver might fail to load (don't have mysql on computer perhaps).


  • @Rondog said:

    There could be a few reasons. First thing to check is that you have the QMYSQL driver:

    QStringList Items = QSqlDatabase::drivers();
    QStringList::const_iterator StrIter;

    qWarning("Available database drivers:");
    for(StrIter = Items.constBegin();StrIter != Items.constEnd();StrIter++)
    {
    qWarning(QString(" Driver: %1\n").arg(*StrIter).toLatin1());
    }

    I checked out but cannot get QMYSQL driver.

    If you don't see QMYSQL then you might have the following problems:

    Don't have the plugin for mysql compiled
    Don't have the plugin for mysql in a location the application can find

    I have MYSQL installed so may be I am facing second problem. Please help me to solve the problem.



  • You need to have the MySql plugin for Qt compiled and installed.

    The source for the plugin relative to the Qt package is qtbase/src/plugins/sqldrivers/mysql If you compile this directly (i.e. command line from this folder: qmake, make) this will create the plugin under qtbase/plugins/sqldrivers. If you can't compile it you might be missing the development libraries and headers for MySql. Just install what is missing and repeat. If everything is compiled properly you should have something like 'libsqlmysql, or libmysql.dll, or similar' in the sqldrivers plugin folder.

    If you are testing your program on the same machine you have Qt installed it should work at this point (Qt can usually find the plugins with this setup). If not, then copy the plugin folder 'sqldrivers' and put this in the same directory as your application. You must do this if you want to run your program on another computer (along with other plugins such as 'platforms', 'imageformats', 'printsupport', ...)



  • Just compile your Mysql Driver

    http://doc.qt.io/qt-5/sql-driver.html#qmysql


  • Lifetime Qt Champion

    Hi,

    What OS are you running ? What version of the MySQL client libraries did you install ?


Log in to reply
 

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