Bonjour,
Je suis sure que ce poste a déjà du être posté 1000x. J'ai fait plein de recherche sur internet dans les formes mais jusqu'à présent tout ce que j'ai pu testé ne fonctionne pas.
Mon bute est d'utiliser mysql dans QT.
J'ai d'abord testé "QMYSQL" utilisant divers tuto. A la fin ca compile mais j'ai cette erreur:
D'après ce que j'ai lu dans les forum c'est libmysql.dll et/ou libmysql.lib qui doit être copié dans un dossier spécifique.QSqlDatabase: QMYSQL driver not loaded
Je l'ai copié dans divers dossiers selon les forms que j'ai consulté. Rien a faire ca ne fonctionne pas.
J'ai donc opté pour ne pas utiliser QMYSQL mais utiliser Connector C++. Je l'utilise dans un programme c++ sous linux qui ne fait pas intervenir QT et ca fonctionne.
Je me suis dit je vais essayer de faire quelque chose que je connais un peu.
Et j'ai trouvé un tuto qui explique comment faire ca dans QT.
J'ai du le modifié pour enlever quelques erreurs de compilation mais la je suis bloqué avec une erreur de compilation que je n'arrive pas à surmonter.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #------------------------------------------------- # # Project created by QtCreator 2021-10-26T10:08:09 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = DataBaseTest TEMPLATE = app INCLUDEPATH += 'C:\Program Files\MySQL\Connector C++ 8.0\include\jdbc' LIBS += -L'C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14 -lmysqlcppconn' # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as 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 you use 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\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui
Voici l'erreur de compilation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 #include "mainwindow.h" #include "ui_mainwindow.h" #include <cppconn/connection.h> #include <cppconn/prepared_statement.h> #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); /////////////////////////// sql::Driver *driver; sql::Connection *connection; try{ driver=get_driver_instance(); connection=driver->connect("localhost", "mano", "mano"); connection->setAutoCommit(0); connection->setSchema("addressbook"); }catch (sql::SQLException &ex) { // cout<<"Exception occurred"<<ex.getErrorCode()<<endl; } //res = stmt->executeQuery("SELECT * FROM Animal"); sql::PreparedStatement *pstmt=connection->prepareStatement("SELECT * FROM Addresses"); sql::ResultSet *resultSet=NULL; try{ resultSet=pstmt->executeQuery(); while(resultSet->next()){ //Person p; int id = resultSet->getInt("addressId"); QString firstName = QString::fromStdString(resultSet->getString("firstName")); QString lastName = QString::fromStdString(resultSet->getString("lastName")); QString email = QString::fromStdString(resultSet->getString("email")); QString phoneNumber = QString::fromStdString(resultSet->getString("phoneNumber")); //list.append(p); } }catch(sql::SQLException &ex){ // cout<<"Exception occurred"<<ex.getErrorCode()<<endl; resultSet->close(); pstmt->close(); delete pstmt; delete resultSet; //return list; } }
D:\Thomas_2nd\QT\build-DataBaseTest-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\mainwindow.o:-1: In function `check_lib':
C:\Program Files\MySQL\Connector C++ 8.0\include\jdbc\cppconn\driver.h:77: erreur : undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:\Program Files\MySQL\Connector C++ 8.0\include\jdbc\cppconn\driver.h:78: erreur : undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
D:\Thomas_2nd\QT\build-DataBaseTest-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\mainwindow.o:-1: In function `get_driver_instance_by_name':
C:\Program Files\MySQL\Connector C++ 8.0\include\jdbc\cppconn\driver.h:90: erreur : undefined reference to `_get_driver_instance_by_name'
collect2.exe:-1: erreur : error: ld returned 1 exit status
Quelqu'un aurait il une idée de comment corriger cette erreur ?
Merci d'avance
Dereck
Partager