0

I'm working under Qt widget Project using qmake , here every thing going will . now i want to switch to cMake , i don't know how i can connect My Application to Mysql under cMake after a long search in the internet i still not found a solution for my problem her My approach i added this lines to My cMakeLists.txt

"find_package(Qt6 COMPONENTS Sql REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::Sql)"

add this line to my header file

#include QSqlDatabase

i created a simple teste function like this

 void MainWindow::testDatabaseinCMake()
 {

     QSqlDatabase dataBase;
     // here you are Data Base Parameters
   dataBase= QSqlDatabase::addDatabase("QMYSQL");
   dataBase.setHostName("127.0.0.1"); // @ip MySql Server
   dataBase.setDatabaseName("mecaniquecalculation"); //Database Name
   dataBase.setUserName("root"); // User Name
   dataBase.setPassword("rrrrr)I"); // Password
   dataBase.setPort(3306);
   
 }

but i still obtain many errors like this QSqlDatabase file not found my be someone can Helpe me thank you in advance

nbk
  • 45,398
  • 8
  • 30
  • 47
kinidi
  • 1
  • 1
  • in can't understand your queszion. Cmake is a tool that uses a script to compile a programm or library, so why doy you want to switch to cmake and what you try to achieve – nbk Feb 21 '22 at 15:35
  • @nbk , thank you for your answer ,https://www.qt.io/blog/qt-and-cmake-the-past-the-present-and-the-future – kinidi Feb 21 '22 at 15:40
  • for many reason i want to switch in to cMake , and now i want to connect to Mysql database with my application but i don't found helpe , how i can do it when we use cMake – kinidi Feb 21 '22 at 15:43
  • see https://stackoverflow.com/questions/69590552/qt-cmake-finds-mysql-but-it-does-not-compile-it here is a functioning cmake script of cpurse see also the comment what os needed., maybe this helps you – nbk Feb 21 '22 at 15:54
  • thank you for your help, here they use ubuntu like OS, I'm using windows OS , i'm not found yet solution – kinidi Feb 22 '22 at 07:18
  • such information belongs to the question see https://stackoverflow.com/questions/6483523/qt-how-to-getcompile-mysql-driver – nbk Feb 22 '22 at 10:58
  • Incredible, i feel i'm the first one using MySQL in Qt cMake – kinidi Feb 22 '22 at 13:16

1 Answers1

0

Place

find_package(Qt6 REQUIRED COMPONENTS Sql)

before this code

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(appName MANUAL_FINALIZATION ${PROJECT_SOURCES})
else()
    if(ANDROID)
        add_library(appName SHARED ${PROJECT_SOURCES})
    else()
        add_executable(appName ${PROJECT_SOURCES})
    endif()
endif() 

and then add

target_link_libraries(appName PRIVATE Qt6::Sql)