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. Qt MySQL Support
QtWS25 Last Chance

Qt MySQL Support

Scheduled Pinned Locked Moved Solved General and Desktop
mysqlmysql driver
8 Posts 4 Posters 4.3k Views
  • 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.
  • M Offline
    M Offline
    mjsbbs
    wrote on last edited by koahnig
    #1

    Hi All, I would like to build a tool using QT which can query an existing MySQL database on a local server. I found this example, but am having trouble getting it to work. After running the example code, I see the error message shown below. Does anyone know what next steps should be to get this to work? Thank you.

    output

    #include <QCoreApplication>
    #include <QApplication>
    #include <QtSql/QSql>
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlDriver>
    #include <QtSql/QSqlQuery>
    #include <QDebug>
    #include <QMessageBox>
    #include <QSqlError>
    
    bool createConnection();
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        if (!createConnection()){
    
            qDebug() << "Not connected!";
            return 1;
        }
        else{
    
            qDebug() << "Connected!";
    
            QSqlQuery query;
            query.exec("SELECT name FROM student");
    
            while (query.next()) {
                QString name = query.value(0).toString();
                qDebug() << "name:" << name;
            }
    
            return 0;
        }
    
        return app.exec();
    }
    
    bool createConnection(){
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("10.0.0.181");
        db.setDatabaseName("dbname");
        db.setUserName("username");
        db.setPassword("password");
        if (!db.open()) {
            qDebug() << "Database error occurred";
    /*
            QMessageBox::critical(0, "", "Cannot open database", QMessageBox::Cancel,
                              QMessageBox::NoButton);
            QMessageBox::critical(0, "", db.lastError().text(), QMessageBox::Cancel,
                     QMessageBox::NoButton);
    */
            return false;
        }
        return true;
    }
    
    
    • Mike
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! This is a common problem. Please use the forum search :-)

      M 1 Reply Last reply
      1
      • ? A Former User

        Hi! This is a common problem. Please use the forum search :-)

        M Offline
        M Offline
        mjsbbs
        wrote on last edited by
        #3

        @Wieland Thank you, Wieland. I have done this but am still struggling, and am hopeful for a bit of guidance. Most discussions involve solving this problem under Linux. I am looking for a Windows solution (which I did fail to mention in my post). Even a small nudge in the right direction would be appreciated.

        This is the tutorial I am following. The tutorial does not say what to do if this error is shown. However, it is indicated that "libmysql.dll" must be present in the "MySQL directory", so maybe that is the problem. I have this file but the tutorial doesn't explain where it needs to be, other than the "MySQL directory" (this could be under the MySQL folder, or it could be under the QT folder, and then under any of several sub folders). It's unclear if QT is finding it but needs something else, or if this is what is needed but it can't find it. Referenced documentation mentions that there is a set of drivers which come with QT. MySQL is listed, so maybe I didn't even need to install MySQL. The error message shown above does show "QMYSQL" as having an available driver. db.lastError().text() shows "Driver not loaded Driver not loaded". Perhaps the driver exists but the path to it needs to be specified. I'm unsure where I would specify the path, though, and the tutorial doesn't do this.

        These are some basic questions I am unsure how to get answers to without help.

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

          Hi,

          Go to the Run part of the Project panel in Qt Creator, there modify the PATH environment variable and add the path to the folder where the MySQL .dll files can be found.

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

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Go to the Run part of the Project panel in Qt Creator, there modify the PATH environment variable and add the path to the folder where the MySQL .dll files can be found.

            M Offline
            M Offline
            mjsbbs
            wrote on last edited by
            #5

            @SGaist Thank you! Adding the path to the folder with the MySQL dll files to the environmental variables path solved the problem.

            Images showing how to add to the path variable from within QT are shown below.

            alt text

            alt text

            alt text

            The path can alternately be added using the System control panel. From the System control panel, I selected "Advanced system settings":
            System control panel

            The "System Properties" window appered. I selected "Environmental Variables":
            System Properties

            From the Environmental Variables window, I selected the "Path" variable and clicked "Edit...":
            Environmental Variables window

            A list of paths appered. I clicked "New":
            list of paths

            An edit field for a new path became available, and I pasted in the path of the folder which contained the libmysql.dll file. (Which is simply the "lib" folder underneath where I unzipped the "mysql-5.7.16-win32" download to.)
            edit field

            When I run the code I posted above, it now reports that it is able to connect to the database:
            results

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

              Don't do that in your system environment variable. That's a bad habit !

              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
              1
              • D Offline
                D Offline
                Diazzzy
                wrote on last edited by
                #7

                Hello, i've the same problem. I'm on Mac and i'm on that problem since 3 days ...

                I tried your method but it doesn't worked :/
                Did someone have a solution?

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

                  Hi and welcome to devnet,

                  Please search the forum a bit, I've already provided the solution several times for OS X/macOS.

                  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
                  1

                  • Login

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