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. QPluginLoader Failed to load qsqlmysql.dll
Forum Updated to NodeBB v4.3 + New Features

QPluginLoader Failed to load qsqlmysql.dll

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 546 Views 1 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.
  • J Offline
    J Offline
    JCherry
    wrote on last edited by JCherry
    #1

    Hi, I'm using Qt 6.5 cmake to build my program. From the beginning till now, I have tried several solutions given in Qt Forum to load MySQL. Below I have mentioned a solution is runnable but convert to exe is not runnable. I have also attached qsqlmysql.dll method below. Please read till below. Anyone helps is appreciated.

    I tried by setting up the path to MySQL Server 8.0 directly and it works. For those that are wondering how did I import MySQL Server 8.0 into my project below is the code.

    cmakelist.txt
    set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0")
    set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
    
    find_library(MYSQL_LIBRARY
        NAMES mysql
        PATHS ${MYSQL_DIR}/lib
    )
    
    target_link_libraries(target
        ${MYSQL_LIBRARY}
    )
    

    Currently I want to deploy my project into exe and it seems that by setting up the path to MySQL Server 8.0 would not work.

    For the second solution, I built qsqlmysql.dll into mingw_64 and copied into my project build. I tried to load the qsqlmysql.dll in my main.cpp but I received and error.

    Failed to load the plugin: "Cannot load library D:\\qmlManualEdi\\build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug\\plugins\\sqldrivers\\qsqlmysql.dll: The specified module could not be found."
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QPSQL QMARIADB QMYSQL
    Failed to connect to MySQL database: "Driver not loaded Driver not loaded"
    

    Attached is also the method on how I load my qsqlmysql.dll

    main.cpp
    // Load MySQL driver
        QString currPath = QDir::currentPath();
        currPath += "/plugins/sqldrivers";
        QCoreApplication::addLibraryPath(currPath);
    
        QString mysqlDll = "qsqlmysql";
    
        QPluginLoader pluginLoader(mysqlDll);
    
        if (pluginLoader.load()) {
            qDebug() << "Plugin loaded successfully!";
            QObject *pluginInstance = pluginLoader.instance();
            if (pluginInstance) {
                // Plugin-specific functionality can be accessed here
            } else {
                qDebug() << "Failed to create plugin instance.";
            }
        } else {
            qDebug() << "Failed to load the plugin:" << pluginLoader.errorString();
        }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher moved this topic from QML and Qt Quick on
    • J JCherry

      Thank you everyone for the suggestions. I managed to set the system PATH. Just like what @Bonnie and @ChrisW67, in order to ensure the mysql is deploy, I need to set up the PATH for mysql. To do so, jut use SET PATH=%PATH%;[mysql/bin].

      J Offline
      J Offline
      JCherry
      wrote on last edited by
      #9

      Here is a final version on how to solve this related matter.

      1. Ensure that you have set your system path for mysql.
      SET PATH=%PATH%;[mysql/bin]
      
      1. In your cmakelist, link mysql library to project
      set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0")
      set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
      
      find_library(MYSQL_LIBRARY
          NAMES mysql
          PATHS ${MYSQL_DIR}/lib
      )
      
      target_link_libraries(target
          ${MYSQL_LIBRARY}
      )
      
      
      1. If you like to deploy the project to other pc, please ensure that you have included necessary file needed for mysql.
        For my case, the mysql version I'm using is MySQL Server 8.0. What I need to do was copy libcrypto-3-x64.dll, libssl-3-x64.dll and libmysql.dll to my project directory. Here a cmakelist version of copy the dlls needed.
      set(MYSQL_REQUIRED_FILES
          "${MYSQL_DIR}/bin/libcrypto-3-x64.dll"
          "${MYSQL_DIR}/bin/libssl-3-x64.dll"
          "${MYSQL_DIR}/lib/libmysql.dll"
      )
      
      foreach(SOURCE_FILE IN LISTS MYSQL_REQUIRED_FILES)
          get_filename_component(FILE_NAME ${SOURCE_FILE} NAME)
          add_custom_command (TARGET appManualEDI POST_BUILD
              COMMAND ${CMAKE_COMMAND} -E copy
                  ${SOURCE_FILE}
                  ${CMAKE_BINARY_DIR}/${FILE_NAME}
              COMMENT "Copying ${FILE_NAME} to ${CMAKE_BINARY_DIR}"
          )
      endforeach()
      
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        Build time and runtime dependencies are not the same thing.

        Your application has to find its dependencies at runtime either because they are present in the same folder as the executable or can be found through the PATH environment variable.

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

        J 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          Build time and runtime dependencies are not the same thing.

          Your application has to find its dependencies at runtime either because they are present in the same folder as the executable or can be found through the PATH environment variable.

          J Offline
          J Offline
          JCherry
          wrote on last edited by
          #3

          @SGaist Hi,
          this is my cmakelist.txt. Is this match what you mentioned about runtime?

          set(QT_SQL_DRIVERS_SOURCE_DIR "D:/MMU/Degree/Intern/iRadar/sourceTree/qmlManualEdi/build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug/sqldrivers")
          set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers")
          
          ...
          
          install(DIRECTORY "${QT_SQL_DRIVERS_SOURCE_DIR}/"
              DESTINATION "${QT_SQL_DRIVERS_INSTALL_DIR}"
          
              FILES_MATCHING PATTERN "*.dll" # Change the pattern if needed
          )
          
          install(TARGETS appManualEDI
              BUNDLE DESTINATION .
          #    CONFIGURATIONS Debug
              LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
              RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
          )
          
          C 1 Reply Last reply
          0
          • J JCherry

            @SGaist Hi,
            this is my cmakelist.txt. Is this match what you mentioned about runtime?

            set(QT_SQL_DRIVERS_SOURCE_DIR "D:/MMU/Degree/Intern/iRadar/sourceTree/qmlManualEdi/build-ManualEDI-Desktop_Qt_6_5_2_MinGW_64_bit-Debug/sqldrivers")
            set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers")
            
            ...
            
            install(DIRECTORY "${QT_SQL_DRIVERS_SOURCE_DIR}/"
                DESTINATION "${QT_SQL_DRIVERS_INSTALL_DIR}"
            
                FILES_MATCHING PATTERN "*.dll" # Change the pattern if needed
            )
            
            install(TARGETS appManualEDI
                BUNDLE DESTINATION .
            #    CONFIGURATIONS Debug
                LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
            )
            
            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @JCherry The Qt Mysql plugin DLL nneeds to be deployed with your program, usually into a /plugins/sqldriver subdirectory. You seem to be doing this.

            At run time, your program will load the Qt Mysql plugin which needs to find the Mysql run time DLL(s), either in the same directory as your program executable or on the system's PATH. The Mysql run time DLL(s) are not part of Qt, they'll likely be somewhere under "C:/Program Files/MySQL/MySQL Server 8.0" on your development machine. It is up to you to deploy those DLLs with your program onto the target machine, or arrange for them to be present another way.

            J 1 Reply Last reply
            0
            • J Offline
              J Offline
              JCherry
              wrote on last edited by
              #5

              Latest Update
              I seems to able to get the qsqlmysql.dll to loaded successfully. All I did was set the path to the plugins and use target_link_libraries. Please take note that I'm not sure how it works but it works.

              cmakelist.txt
              set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers")
              
              target_link_libraries(appManualEDI
                  ${MYSQL_LIBRARY}
              )
              
              // Load MySQL driver
                  QString currPath = QDir::currentPath();
                  currPath += "/plugins/sqldrivers";
                  QCoreApplication::addLibraryPath(currPath);
              
                  QString mysqlDll = "qsqlmysql";
              
                  QPluginLoader pluginLoader(mysqlDll);
              
                  if (pluginLoader.load()) {
                      qDebug() << "Plugin loaded successfully!";
                      QObject *pluginInstance = pluginLoader.instance();
                      if (pluginInstance) {
                          // Plugin-specific functionality can be accessed here
                      } else {
                          qDebug() << "Failed to create plugin instance.";
                      }
                  } else {
                      qDebug() << "Failed to load the plugin:" << pluginLoader.errorString();
                  }
              

              But when deploy to executable file it still does not work. My guess is that it won't load in executable file is because of my install part doesnt seem to install the qsqlmysql.dll. But I'm not sure how to do it.

              B 1 Reply Last reply
              0
              • C ChrisW67

                @JCherry The Qt Mysql plugin DLL nneeds to be deployed with your program, usually into a /plugins/sqldriver subdirectory. You seem to be doing this.

                At run time, your program will load the Qt Mysql plugin which needs to find the Mysql run time DLL(s), either in the same directory as your program executable or on the system's PATH. The Mysql run time DLL(s) are not part of Qt, they'll likely be somewhere under "C:/Program Files/MySQL/MySQL Server 8.0" on your development machine. It is up to you to deploy those DLLs with your program onto the target machine, or arrange for them to be present another way.

                J Offline
                J Offline
                JCherry
                wrote on last edited by
                #6

                @ChrisW67
                i get what you mean. But how do i deploy the dlls in my runtime to ensure the executable file also includes the dlls.

                1 Reply Last reply
                0
                • J JCherry

                  Latest Update
                  I seems to able to get the qsqlmysql.dll to loaded successfully. All I did was set the path to the plugins and use target_link_libraries. Please take note that I'm not sure how it works but it works.

                  cmakelist.txt
                  set(QT_SQL_DRIVERS_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/plugins/sqldrivers")
                  
                  target_link_libraries(appManualEDI
                      ${MYSQL_LIBRARY}
                  )
                  
                  // Load MySQL driver
                      QString currPath = QDir::currentPath();
                      currPath += "/plugins/sqldrivers";
                      QCoreApplication::addLibraryPath(currPath);
                  
                      QString mysqlDll = "qsqlmysql";
                  
                      QPluginLoader pluginLoader(mysqlDll);
                  
                      if (pluginLoader.load()) {
                          qDebug() << "Plugin loaded successfully!";
                          QObject *pluginInstance = pluginLoader.instance();
                          if (pluginInstance) {
                              // Plugin-specific functionality can be accessed here
                          } else {
                              qDebug() << "Failed to create plugin instance.";
                          }
                      } else {
                          qDebug() << "Failed to load the plugin:" << pluginLoader.errorString();
                      }
                  

                  But when deploy to executable file it still does not work. My guess is that it won't load in executable file is because of my install part doesnt seem to install the qsqlmysql.dll. But I'm not sure how to do it.

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #7

                  @JCherry Runtime environments is total different from build time.
                  Any path you set in the cmakelist.txt will only affect build time.
                  The reason you can run it in IDEs is because they are defaultly set to copy build time library search path to runtime PATH.
                  But when you double click the exe to run, the runtime PATH is not set to include that.
                  So you either set the runtime PATH (by 1. setting the system's PATH or 2.write a bat/cmd file, set PATH in it and then start the exe from that), or also copy Mysql dlls with your exe as @ChrisW67 mentioned.

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    JCherry
                    wrote on last edited by
                    #8

                    Thank you everyone for the suggestions. I managed to set the system PATH. Just like what @Bonnie and @ChrisW67, in order to ensure the mysql is deploy, I need to set up the PATH for mysql. To do so, jut use SET PATH=%PATH%;[mysql/bin].

                    J 1 Reply Last reply
                    0
                    • J JCherry

                      Thank you everyone for the suggestions. I managed to set the system PATH. Just like what @Bonnie and @ChrisW67, in order to ensure the mysql is deploy, I need to set up the PATH for mysql. To do so, jut use SET PATH=%PATH%;[mysql/bin].

                      J Offline
                      J Offline
                      JCherry
                      wrote on last edited by
                      #9

                      Here is a final version on how to solve this related matter.

                      1. Ensure that you have set your system path for mysql.
                      SET PATH=%PATH%;[mysql/bin]
                      
                      1. In your cmakelist, link mysql library to project
                      set(MYSQL_DIR "C:/Program Files/MySQL/MySQL Server 8.0")
                      set(MYSQL_INCLUDE_DIR "${MYSQL_DIR}/include")
                      
                      find_library(MYSQL_LIBRARY
                          NAMES mysql
                          PATHS ${MYSQL_DIR}/lib
                      )
                      
                      target_link_libraries(target
                          ${MYSQL_LIBRARY}
                      )
                      
                      
                      1. If you like to deploy the project to other pc, please ensure that you have included necessary file needed for mysql.
                        For my case, the mysql version I'm using is MySQL Server 8.0. What I need to do was copy libcrypto-3-x64.dll, libssl-3-x64.dll and libmysql.dll to my project directory. Here a cmakelist version of copy the dlls needed.
                      set(MYSQL_REQUIRED_FILES
                          "${MYSQL_DIR}/bin/libcrypto-3-x64.dll"
                          "${MYSQL_DIR}/bin/libssl-3-x64.dll"
                          "${MYSQL_DIR}/lib/libmysql.dll"
                      )
                      
                      foreach(SOURCE_FILE IN LISTS MYSQL_REQUIRED_FILES)
                          get_filename_component(FILE_NAME ${SOURCE_FILE} NAME)
                          add_custom_command (TARGET appManualEDI POST_BUILD
                              COMMAND ${CMAKE_COMMAND} -E copy
                                  ${SOURCE_FILE}
                                  ${CMAKE_BINARY_DIR}/${FILE_NAME}
                              COMMENT "Copying ${FILE_NAME} to ${CMAKE_BINARY_DIR}"
                          )
                      endforeach()
                      
                      
                      1 Reply Last reply
                      0
                      • J JCherry has marked this topic as solved on

                      • Login

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