QSqlDatabase "Driver not loaded" 🤔🤨
-
This:
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
Should not, according to the documentation, cause any problems related to what Im experiencing.
My project is stored in OneDrive, along with my build files. I have now removed every trace of sql there, yet I still get the same error messages. If there was a problem with Qt and OneDrive, I would be having more issues, so I doubt that is the culprit.
Main.cpp
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qDebug() << QSqlDatabase::drivers(); const QUrl url(u"qrc:/x/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); Database db("QMYSQL", "127.0.0.1", "x", "y", "z"); return app.exec(); }
Database.h
#ifndef DATABASE_H #define DATABASE_H #include <QObject> #include <QtSql/QSqlDriver> #include <QtSql> #include <QDebug> #include <QSqlDatabase> #include <QSqlError> class Database : public QObject { Q_OBJECT public: explicit Database(const QString& driverType, const QString& hostName, const QString& dbName, const QString& userName, const QString& password, QObject* parent = nullptr); ~Database(); private: QSqlDatabase m_db; }; #endif // DATABASE_H
Database.cpp
Database::Database(const QString &driverType, const QString &hostName, const QString &dbName, const QString &userName, const QString &password, QObject* parent) : QObject(parent) { if (!QSqlDatabase::drivers().contains(driverType)) { qDebug() << driverType + " does not exist."; throw std::runtime_error{driverType.toStdString() + " does not exist."}; this->~Database(); exit(EXIT_FAILURE); } m_db = QSqlDatabase::addDatabase(driverType); if (!m_db.isValid()) { throw std::runtime_error{m_db.lastError().text().toStdString()}; this->~Database(); exit(EXIT_FAILURE); } m_db.addDatabase(driverType); m_db.setHostName(hostName); m_db.setDatabaseName(dbName); m_db.setUserName(userName); m_db.setPassword(password); if (!m_db.open()) { qDebug() << m_db.lastError().text(); throw std::runtime_error{"Error: " + m_db.lastError().text().toStdString()}; this->~Database(); exit(EXIT_FAILURE); } }
@CodeBroker
Never had theQSqlDatabasePrivate::*Database...
messages. I suspect they could come from your member variableQSqlDatabase m_db;
, which I am sure when @SGaist sees he will ask you to remove and follow what the docs recommend!I'm not sure just where your plain
Driver not loaded
messages come from/where in the sequence of your code execution. I'm also not sure what the consequences are of trying to add the same database driver twice. I understand you may not be getting as far as my points. Maybe someone else will know better.You have a bunch of other available drivers: does the driver load on any of them?
-
C Christian Ehrlicher moved this topic from QML and Qt Quick on
-
@CodeBroker
Never had theQSqlDatabasePrivate::*Database...
messages. I suspect they could come from your member variableQSqlDatabase m_db;
, which I am sure when @SGaist sees he will ask you to remove and follow what the docs recommend!I'm not sure just where your plain
Driver not loaded
messages come from/where in the sequence of your code execution. I'm also not sure what the consequences are of trying to add the same database driver twice. I understand you may not be getting as far as my points. Maybe someone else will know better.You have a bunch of other available drivers: does the driver load on any of them?
Use a proper init function instead doing strange stuff in the ctor. And according to the debug output you somewhere open the database twice.
Please provide a minimal, compileable example without any qml stuff to reproduce the problem. -
Use a proper init function instead doing strange stuff in the ctor. And according to the debug output you somewhere open the database twice.
Please provide a minimal, compileable example without any qml stuff to reproduce the problem.Im not sure what strange stuff you're referring to, please enlighten me. And, even if strange, how would that cause this issue?
App works fine without initializing the class.
-
Im not sure what strange stuff you're referring to, please enlighten me. And, even if strange, how would that cause this issue?
App works fine without initializing the class.
@CodeBroker said in QSqlDatabase "Driver not loaded" 🤔🤨:
not sure what strange stuff you're referring
The exceptions where you try to call the dtor after throwing. Luckily the dtor is never called due to the throw. It's simply bad design not doing this in a separate init function.
And you somewhere open the db a second time so reduce your code until the message about the removeDatabase problem goes away or provide a minimal, compileable example so we can take a look on it. -
To add to what @Christian-Ehrlicher says re. the duplicate calls to addDateBase(), try removing the 2nd one, say something like this:
.. m_db = QSqlDatabase::addDatabase(driverType); if (!m_db.isValid()) return; // comment out the 2nd invocation // m_db.addDatabase(driverType); m_db.setHostName(hostName); ...
-
Im not sure what strange stuff you're referring to, please enlighten me. And, even if strange, how would that cause this issue?
App works fine without initializing the class.
@CodeBroker @JonB is correct, don't store a QSqlDatabase object as member variable. It's explained in the documentation.
As @hskoglund wrote, your second call to addDatabase is what triggers the message. By the way addDatabase is a static method, it won't change your m_db object.
-
To add to what @Christian-Ehrlicher says re. the duplicate calls to addDateBase(), try removing the 2nd one, say something like this:
.. m_db = QSqlDatabase::addDatabase(driverType); if (!m_db.isValid()) return; // comment out the 2nd invocation // m_db.addDatabase(driverType); m_db.setHostName(hostName); ...
-
-
@JonB said in QSqlDatabase "Driver not loaded" 🤔🤨:
I'm also not sure what the consequences are of trying to add the same database driver twice
Nobody reads what I write in the first place! '-(
-
Hi I'm new to Qt and GUI programming and development. I'M a UX and UI Designer trying my hand at actually coding a complex software I have developped. SO I have to start from the beginnign and I'm following a book to get my hands and branin wrapped around these new concepts and I have encountered a first error that I have been trying to figure out with the help of Chat GPT but I have come to the end of what I can do alone.
here is the problem.
I have installed MAMP to manage the webserver and the MYSQL databse.
I have created some tables and populated them
Now i want to connect the GUI i have designed with Qt (A simple login form)
but I get this error messag when trying to compile12:08:00: Starting /Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/build-DatabaseConn-Qt_6_8_0_for_macOS-Debug/DatabaseConn... 12:08:00: Environment: __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0 __CFBundleIdentifier=org.qt-project.qtcreator COMMAND_MODE=unix2003 DYLD_FRAMEWORK_PATH=/Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/DatabaseConn:/usr/local/Cellar/mysql/8.3.0_1/lib/libmysqlclient.dylib:/Users/sebastienmarchi/Qt6/6.8.0/macos/lib DYLD_LIBRARY_PATH=/Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/DatabaseConn:/usr/local/Cellar/mysql/8.3.0_1/lib/libmysqlclient.dylib:/Users/sebastienmarchi/Qt6/6.8.0/macos/lib HOME=/Users/sebastienmarchi LOGNAME=sebastienmarchi PATH=/Users/sebastienmarchi/Qt6/6.8.0/macos/bin:/usr/bin:/bin:/usr/sbin:/sbin QTDIR=/Users/sebastienmarchi/Qt6/6.8.0/macos SHELL=/bin/bash SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.gWvkjBV314/Listeners TMPDIR=/var/folders/hh/zt4mxsmd3hg8qfhx18wrtpw80000gn/T/ USER=sebastienmarchi XPC_FLAGS=0x0 XPC_SERVICE_NAME=application.org.qt-project.qtcreator.124158950.124214104 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC QPSQL QMIMER Failed to connect. 12:08:00: /Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/build-DatabaseConn-Qt_6_8_0_for_macOS-Debug/DatabaseConn exited with code 0
Using :
Qt Creator 12.0.2
Based on Qt 6.6.0 (Clang 13.0 (Apple), x86_64)Built on Feb 6 2024 10:31:14
-
Hi I'm new to Qt and GUI programming and development. I'M a UX and UI Designer trying my hand at actually coding a complex software I have developped. SO I have to start from the beginnign and I'm following a book to get my hands and branin wrapped around these new concepts and I have encountered a first error that I have been trying to figure out with the help of Chat GPT but I have come to the end of what I can do alone.
here is the problem.
I have installed MAMP to manage the webserver and the MYSQL databse.
I have created some tables and populated them
Now i want to connect the GUI i have designed with Qt (A simple login form)
but I get this error messag when trying to compile12:08:00: Starting /Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/build-DatabaseConn-Qt_6_8_0_for_macOS-Debug/DatabaseConn... 12:08:00: Environment: __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0 __CFBundleIdentifier=org.qt-project.qtcreator COMMAND_MODE=unix2003 DYLD_FRAMEWORK_PATH=/Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/DatabaseConn:/usr/local/Cellar/mysql/8.3.0_1/lib/libmysqlclient.dylib:/Users/sebastienmarchi/Qt6/6.8.0/macos/lib DYLD_LIBRARY_PATH=/Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/DatabaseConn:/usr/local/Cellar/mysql/8.3.0_1/lib/libmysqlclient.dylib:/Users/sebastienmarchi/Qt6/6.8.0/macos/lib HOME=/Users/sebastienmarchi LOGNAME=sebastienmarchi PATH=/Users/sebastienmarchi/Qt6/6.8.0/macos/bin:/usr/bin:/bin:/usr/sbin:/sbin QTDIR=/Users/sebastienmarchi/Qt6/6.8.0/macos SHELL=/bin/bash SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.gWvkjBV314/Listeners TMPDIR=/var/folders/hh/zt4mxsmd3hg8qfhx18wrtpw80000gn/T/ USER=sebastienmarchi XPC_FLAGS=0x0 XPC_SERVICE_NAME=application.org.qt-project.qtcreator.124158950.124214104 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC QPSQL QMIMER Failed to connect. 12:08:00: /Users/sebastienmarchi/Qt Projects/2024 01 Sandbox2/build-DatabaseConn-Qt_6_8_0_for_macOS-Debug/DatabaseConn exited with code 0
Using :
Qt Creator 12.0.2
Based on Qt 6.6.0 (Clang 13.0 (Apple), x86_64)Built on Feb 6 2024 10:31:14