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. trouble with database connection first try / time - never before connected
Forum Updated to NodeBB v4.3 + New Features

trouble with database connection first try / time - never before connected

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.0k 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.
  • L Offline
    L Offline
    lse123
    wrote on last edited by
    #1

    Tried installed XAMPP at 3306 apache/ mysql server and also installed from Oracle/MySQL mysql server 8.0 at 33601 and connector C++
    where put libmysql.dll from Oracle MySQL server install /? what is thisa mysqlcppconn8-2-vs14.dll from connector? // tried all and maria DB but getting same message well ?

    Console
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
    Failed to connect.
    Press <RETURN> to close this window...

    code main.cpp console app ---
    DatabaseConnection.pro

    QT += core sql
    #QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
            main.cpp
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    // main.cpp

    #include <QCoreApplication>
    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); // QMARIADB
        db.setHostName("127.0.0.1");
        db.setPort(33061);  // <<< MysQL Server 8.0 |  XAMPP MySQL 3306 
        db.setDatabaseName("test");
        db.setUserName("root");
        db.setPassword("123456"); //    XAMPP Mysql empty pass  ''
    
        if (db.open())
        {
            qDebug() << "Connected!";
        }
        else
        {
            qDebug() << "Failed to connect.";
            return 0;
        }
    
    
      QString command = "SELECT my_user.username, department.name AS deptname FROM (SELECT * FROM user WHERE status = 0) AS my_user INNER JOIN department ON department.id = my_user.deptID";
        QSqlQuery query(db);
        if (query.exec(command))
        {
            while(query.next())
            {
                QString username = query.value("username").toString();
                QString department = query.value("deptname").toString();
    
                qDebug() << username << department;
            }
        }
    
        return a.exec();
    }
    

    0_1556618399422_2019-04-30_12-56-19 Qt forums.png

    A 1 Reply Last reply
    0
    • L lse123

      Tried installed XAMPP at 3306 apache/ mysql server and also installed from Oracle/MySQL mysql server 8.0 at 33601 and connector C++
      where put libmysql.dll from Oracle MySQL server install /? what is thisa mysqlcppconn8-2-vs14.dll from connector? // tried all and maria DB but getting same message well ?

      Console
      QSqlDatabase: QMYSQL driver not loaded
      QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
      Failed to connect.
      Press <RETURN> to close this window...

      code main.cpp console app ---
      DatabaseConnection.pro

      QT += core sql
      #QT -= gui
      
      CONFIG += c++11 console
      CONFIG -= app_bundle
      
      # The following define makes your compiler emit warnings if you use
      # any Qt feature that has been marked deprecated (the exact warnings
      # depend on your compiler). Please consult the documentation of the
      # deprecated API in order to know how to port your code away from it.
      DEFINES += QT_DEPRECATED_WARNINGS
      
      # You can also make your code fail to compile if it uses deprecated APIs.
      # In order to do so, uncomment the following line.
      # You can also select to disable deprecated APIs only up to a certain version of Qt.
      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
      
      SOURCES += \
              main.cpp
      
      # Default rules for deployment.
      qnx: target.path = /tmp/$${TARGET}/bin
      else: unix:!android: target.path = /opt/$${TARGET}/bin
      !isEmpty(target.path): INSTALLS += target
      

      // main.cpp

      #include <QCoreApplication>
      #include <QtSql>
      #include <QSqlDatabase>
      #include <QSqlQuery>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); // QMARIADB
          db.setHostName("127.0.0.1");
          db.setPort(33061);  // <<< MysQL Server 8.0 |  XAMPP MySQL 3306 
          db.setDatabaseName("test");
          db.setUserName("root");
          db.setPassword("123456"); //    XAMPP Mysql empty pass  ''
      
          if (db.open())
          {
              qDebug() << "Connected!";
          }
          else
          {
              qDebug() << "Failed to connect.";
              return 0;
          }
      
      
        QString command = "SELECT my_user.username, department.name AS deptname FROM (SELECT * FROM user WHERE status = 0) AS my_user INNER JOIN department ON department.id = my_user.deptID";
          QSqlQuery query(db);
          if (query.exec(command))
          {
              while(query.next())
              {
                  QString username = query.value("username").toString();
                  QString department = query.value("deptname").toString();
      
                  qDebug() << username << department;
              }
          }
      
          return a.exec();
      }
      

      0_1556618399422_2019-04-30_12-56-19 Qt forums.png

      A Offline
      A Offline
      arsinte_andrei
      wrote on last edited by
      #2

      @lse123 said in trouble with database connection first try / time - never before connected:

      Console
      QSqlDatabase: QMYSQL driver not loaded
      QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
      Failed to connect.
      Press <RETURN> to close this window...

      that means that the Mysql dll is not loaded or is missing - Qt cannot find the library to load MySQL - is just a mater of loading /finding the dll and put it in the right director -sorry I'm a Linux user I do not know on windows but you can google it

      https://duckduckgo.com/?q=QSqlDatabase%3A+QMYSQL+driver+not+loaded&t=canonical&atb=v137-1&ia=web

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lse123
        wrote on last edited by lse123
        #3

        @lse123 said in trouble with database connection first try / time - never before connected:

        libmysql.dll from Oracle MySQL server install /? what is thisa mysqlcppconn8-2-vs14.dll

        I have to put this
        libmysql.dll
        or this
        mysqlcppconn8-2-vs14.dll
        or
        libmariadb.dll

        in the right directory? 32-bit vs 64-bit driver connector depends only from mysql installation or and from Qt app if 32/64 [for xampp choose 32-bit connector???]???
        for connection with XAMPP apache / mysql ???

        0_1556623291489_2019-04-30_14-18-59 phpmyadmin.png

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lse123
          wrote on last edited by
          #4

          see and above

          what is the correct directory to put the dll??? this C:\Qt\5.12.2\msvc2017\bin\ or in the app build itself... ?

          0_1556623684753_2019-04-30_14-27-16 powershell.png

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lse123
            wrote on last edited by
            #5

            C:\Qt\5.12.2\msvc2017_64\bin\

            ^^^ put here
            started XAMPP

            connected !!!!
            0_1556624435005_2019-04-30_14-38-36 64 connected.png

            0_1556624743379_2019-04-30_14-45-02 qt result.png

            1 Reply Last reply
            1
            • A Offline
              A Offline
              arsinte_andrei
              wrote on last edited by
              #6

              if you solved the problem please mark this topic as solved - in order for others to know that this has been sorted out...

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lse123
                wrote on last edited by lse123
                #7

                yes SOLVED
                how mark this as SOLVED ?????? OK FOUND IT

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  lse123
                  wrote on last edited by
                  #8

                  driver connector -C dll mysql or mariadb
                  either 32-bit or 64-bit this (the x-bit) must match the y-bit of mysql installation ???

                  In other words 64-bit MySQL installation REQUIRES 64-bit connector-C++ ... ?

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

                    Hi,

                    AFAIK, no, but it has to match the architecture of your version of Qt.

                    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