QODBC Driver not loaded
-
Hi.
I'm using QT 5.15.2 on Win 11 x 64 and trying to connect to the MS SQL Server database. There is a problem with the QODBC driver. When I try to execute db->open() I get the message "Driver not loaded Driver not loaded".Here's the code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSqlDatabase> #include <QSqlError> #include <QObject> #include <QMessageBox> #include <iostream> #include <QStringList> #include <QDebug> #include <QtSql> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); auto *passEdit = ui->lineEdit; auto *label = ui->label; auto *connectButton = ui->pushButton; QString db_password; QString mes_text = ""; if (!QSqlDatabase::drivers().contains("QODBC")) mes_text = "not "; QMessageBox::warning(this, "QODBC test", "QODBC driver " + mes_text + "founded"); //for (auto &Str : QSqlDatabase::drivers()) // std::cout << Str.toStdString() << std::endl; QSqlDatabase *db = new QSqlDatabase; db->addDatabase("QODBC"); QString connectionString = "Driver={SQL Server};"; connectionString.append("Server=RS-FS-03,1433;"); connectionString.append("Database=TEST_DB;"); connectionString.append("Uid=USER;"); connectionString.append("Pwd=PASSWORD"); db->setDatabaseName(connectionString); QObject::connect(passEdit, &QLineEdit::textEdited, [&db_password, passEdit](){db_password = passEdit->text();}); QObject::connect(connectButton, &QPushButton::clicked, [db, label](){ if (db->open()) { label->setText("Connected"); } else { label->setText("Not connected"); qDebug() << db->lastError().text(); } }); } MainWindow::~MainWindow() { delete ui; }
Previously, to install the driver, I ran:
\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers>qmake
\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers>mingw32-make
\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers>mingw32-make installand the driver, it seems to me, has been installed.
I checked in the folder \Qt\5.15.2\mingw81_64\plugins\sqldrivers
qsqlodbc.dll appeared.
The list of supported QODBC drivers includes:
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
Please help me solve the problem. -
@JonB
Thank you, JonB. I added in main.cpp such string, as you proposed:
qputenv("QT_DEBUG_PLUGINS", QByteArray("1"));
After myapp restart I've got the next output:QFactoryLoader::QFactoryLoader() checking directory path ".../Documents/QTProjects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/sqldrivers" ... QFactoryLoader::QFactoryLoader() looking at ".../Documents/QTProjects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/sqldrivers/qsqlodbc.dll" Found metadata in lib .../Documents/QTProjects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/sqldrivers/qsqlodbc.dll, metadata= { "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface", "MetaData": { "Keys": [ "QODBC3", "QODBC" ] }, "archreq": 0, "className": "QODBCDriverPlugin", "debug": false, "version": 331520 } Got keys from plugin meta data ("QODBC3", "QODBC") loaded library "D:/Qt/5.15.2/mingw81_64/plugins/sqldrivers/qsqlodbc.dll" QFactoryLoader::QFactoryLoader() checking directory path "D:/Qt/5.15.2/mingw81_64/plugins/accessible" ... QFactoryLoader::QFactoryLoader() checking directory path ".../Documents/QTProjects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/accessible" ... QFactoryLoader::QFactoryLoader() checking directory path "D:/Qt/5.15.2/mingw81_64/plugins/accessiblebridge" ... QFactoryLoader::QFactoryLoader() checking directory path ".../Documents/QTProjects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/accessiblebridge" ...
After that I click the connectButton and get output:
"Driver not loaded Driver not loaded"
Thus I got more information, but still don't understand why "driver not loaded".
-
@it-aint-me Please compare what you have done:
QSqlDatabase *db = new QSqlDatabase; db->addDatabase("QODBC"); QString connectionString = "Driver={SQL Server};"; connectionString.append("Server=RS-FS-03,1433;"); connectionString.append("Database=TEST_DB;"); connectionString.append("Uid=USER;"); connectionString.append("Pwd=PASSWORD"); db->setDatabaseName(connectionString);
with the example in the QSqlDatabase documentation.
QSqlDatabase::addDatabase() is a static factory method. You need to capture the return value: that is your connection. There is no need for heap allocation and you are warned about keeping the QSqlDatabase longer than necessary to achieve the immediate task.
-
@it-aint-me said in QODBC Driver not loaded:
Projects/FactoryWorkResults/Slaughter_Results_5-12/build/Desktop_Qt_5_15_2_MinGW_64_bit-Debug/debug/sqldrivers/qsqlodbc.dll
Why is the odbc plugin in this directory and not where it belongs to (Qt plugins dir) and why do you compile it by yourself when it was already provided by Qt. Remove your self compiled plugin from there and start over. Then the correct one from the Qt sql plugin directory should be picked. Please post the full output of QT_DEBUG_PLUGINS.
-
Try to install AccessDatabaseEngine Software in your system and Check it.
if not installed then Follow the link for download
https://www.microsoft.com/en-us/download/details.aspx?id=54920 -
@Christian-Ehrlicher said in QODBC Driver not loaded:
Why is the odbc plugin in this directory and not where it belongs to (Qt plugins dir)
I don't know. It appeared after executing mingw32-make install
and here too: \Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers\plugins\sqldrivers
It seems strange for me. I thought it should appear in folder \Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers\odbc@Christian-Ehrlicher said in QODBC Driver not loaded:
why do you compile it by yourself when it was already provided by Qt.
May be, but it didn't work before manual installation. Now it works, but only if I create DSN, and specify it in db.setDatabaseName("USER_DSN");