当前位置:网站首页>QT client development -- driver loading problem of connecting to MySQL database

QT client development -- driver loading problem of connecting to MySQL database

2022-06-13 04:46:00 Shallow as the breeze CYF

Preface

  • For rigid contact Qt For the developers , Connecting to a database is a headache , I have just come into contact with Qt I also encountered many pits , In connection mysql It took two or three days to solve the database problem , This document records the solution to Qt Connect mysql It's a driving problem , Hope to help more developers avoid detours !
  • If it helps you , You might as well click three times !
  • This article is the experience summary of bloggers ! Some operations refer to the articles of other bloggers , But because of the long time , Original link not saved , If you have any questions, I attach a reference link !

resolvent 1: Use mysql The compiled driver in the installation directory 【 Not necessarily effective 】

because mysql Drive for all Qt Versions are universal , therefore , This method may not be effective . It's been tested before ,Qt5.9 MSVC2017 and mysql5.7 It works , Others are invalid

  • Method : take mysql Of lib Copy the dynamic library in the folder to Qt Corresponding to the compiler bin Under the directory
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
error open database "Driver not loaded Driver not loaded"

resolvent 2: Manually compile mysql【 Match the compiler used 】

  • Method :

    • ① Get into Qt Source directory 【 install Qt You need to choose installation source This directory is available only 】:D:\DevEnv\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers\mysql

    • ② modify mysql.pro file

      #  Add the following information , And comment out QMAKE_USE += mysql
      INCLUDEPATH +="D:\DevEnv\mysql-8.0.23-winx64\include"  # mysql Header Directory 
      LIBS +="D:\DevEnv\mysql-8.0.23-winx64\lib\libmysql.lib"  # mysql Library file directory 
      DESTDIR  = ../mysql/lib/  #  Specify the output location after compilation 
      

       Insert picture description here

    • ③ compile
       Insert picture description here

        >  Use Qt 5.15.2 MinGW 64bit compile , The generated dynamic library is only suitable for this compiler 
      
    • There's a problem 【 It seems that this problem can be ignored 】
       Insert picture description here

      • terms of settlement , Modify the following documents :
         Insert picture description here
  • compile MSVC There may be :Qt And msvc-version.conf loaded but QMAKE_MSC_VER isn’t set

    terms of settlement : stay D:\DevEnv\Qt\5.15.2\msvc2019_64\mkspecs\common Of msvc-version.conf Add... To the configuration file QMAKE_MSC_VER = 1900 that will do
     Insert picture description here

    • Compile again , Generate lib Folder 【MinGW and MSVC The generated files are different 】

     Insert picture description here

    • Copy lib Folder files to

       Insert picture description here

      Be careful : This method also needs to copy mysql/lib Files in directory to Qt Of bin Under the table of contents

  • Connect the test

    qDebug() << QSqlDatabase::drivers();
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("wordmemory");
    db.setUserName("root");
    db.setPassword("123456");
    bool ok = db.open();
    
    if (ok) qDebug() << " Connect to database ok";
    else qDebug() << " Failed to connect to database ";
    

 Insert picture description here

原网站

版权声明
本文为[Shallow as the breeze CYF]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280520517896.html