Solved trouble with database connection first try / time - never before connected
-
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.proQT += 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(); }
-
@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
-
@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.dllin 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 ??? -
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... ?
-
C:\Qt\5.12.2\msvc2017_64\bin\
^^^ put here
started XAMPPconnected !!!!
-
if you solved the problem please mark this topic as solved - in order for others to know that this has been sorted out...
-
yes SOLVED
how mark this as SOLVED ?????? OK FOUND IT -
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++ ... ?
-
Hi,
AFAIK, no, but it has to match the architecture of your version of Qt.