Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error while connecting mysql with qt
Forum Updated to NodeBB v4.3 + New Features

Error while connecting mysql with qt

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charu
    wrote on last edited by SGaist
    #1

    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]

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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).
      1 Reply Last reply
      0
      • C Offline
        C Offline
        Charu
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          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', ...)

          1 Reply Last reply
          0
          • Malek_KhlifM Offline
            Malek_KhlifM Offline
            Malek_Khlif
            wrote on last edited by
            #5

            Just compile your Mysql Driver

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

            I Love Qt &lt;3

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved