Qt SQL doesn't detect any driver
-
wrote on 6 Apr 2020, 15:40 last edited by jay1 4 Jun 2020, 15:41
I added the ```
QT += testlib core gui sql
to my pro file but when I try to get the driver list by
QSqlDatabase::drivers()
I get an empty list. Can any one suggest how can I debug this issue.
-
hi
What platform and Qt version ?
How did you install Qt.SqLite should always be included so not really sure how it can go missing.
-
wrote on 6 Apr 2020, 16:43 last edited by
I am using the
Qt Creator Version 4.11.1, Qt 5.14.1 (MSVC 2017, 32 bit) on windows
I installed Qt using the Qt Installer and updated using the Maintainance.exe tool.
-
Hgi
Ok, that should not be possible that it's not included. o.O
Could you try to use it ?bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); // db.setDatabaseName("c:\\folder\\test.db"); if (!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel); return false; } QSqlQuery query; qDebug() << "table:" << query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20), num int )"); query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')"); query.exec("insert into person values(102, 'Christine', 'Holand','2')"); query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')"); return true; }
-
Hi,
Is it the About Qt Creator dialog content ?
If so, we need the version of Qt you are using in your kit. -
wrote on 6 Apr 2020, 18:20 last edited by
Yes it is from the Qt Creator about content.
The version of the Qt from the kit I am using is
Qt 5.14.0 MSVC2017 64bit
for building my application
-
Can you check the content of the sqldrivers folder in the plugins directory of that Qt version ?
-
Hgi
Ok, that should not be possible that it's not included. o.O
Could you try to use it ?bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); // db.setDatabaseName("c:\\folder\\test.db"); if (!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel); return false; } QSqlQuery query; qDebug() << "table:" << query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20), num int )"); query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')"); query.exec("insert into person values(102, 'Christine', 'Holand','2')"); query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')"); return true; }
wrote on 6 Apr 2020, 18:33 last edited by@mrjj I tried the code you provided with one change i.e. removed the QMessageBox and printed the qDebug() message. The code i tried is as follows followed with the output:
#include <QDebug> #include <QSqlDatabase> #include <QMessageBox> #include <QSqlQuery> bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); // db.setDatabaseName("c:\\folder\\test.db"); if (!db.open()) { qDebug() << "Failed to open the database"; return false; } QSqlQuery query; qDebug() << "table:" << query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20), num int )"); query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')"); query.exec("insert into person values(102, 'Christine', 'Holand','2')"); query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')"); return true; } int main(int argc, char *argv[]) { Q_UNUSED(argc) Q_UNUSED(argv) qDebug() << "List the drivers: " << QSqlDatabase::drivers(); createConnection(); return EXIT_SUCCESS; } ''' OUTPUT: ''' List the drivers: () QSqlDatabase: QSQLITE driver not loaded QSqlDatabase: available drivers: QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins Failed to open the database
I find this "QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins" statement kind of related as I had not created QCoreApplication in this scenario and also in my actual scenario there is not QCoreApplication whereI am using the QTest. But I think I must be able to create the QTest with sql drivers loaded , Please correct me if I am wrong?
-
Can you check the content of the sqldrivers folder in the plugins directory of that Qt version ?
wrote on 6 Apr 2020, 18:44 last edited by@SGaist In the Qt directory ```
C:\Qt\5.14.0\msvc2017_64\plugins\sqldriversI was able to find the files 1. qsqlite.dll 2. qsqlodbc.dll 3. qsqlpsql.dll and this 1. qsqlited.dll 2. qsqlodbcd.dll 3. qsqlpsqld.dll
And also I use "windeployqt" for deploying and have the "sqldrives" directory along with my executable which have the above dll sql files
-
Start your application with the QT_DEBUG_PLUGINS environment variable set to 1.
It should give you the reason of the failure.
-
wrote on 6 Apr 2020, 19:03 last edited by
@SGaist I added the QT_DEBUG_PLUGINS = 1 to environment variables. And when I build I was not able to get any log information in Application output. But when I added the
QAppication a(argc, argv)
I was able to see the log related to SQLITE Plugin metadata.
After the addition of the "QApplication a(argc, argv)" I was able to retrieve/get the list of drives SQL drivers
List the drivers: ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
I think to load sql plugins we need the QCoreApplication to be triggered, need to digup more about about the plugins loading.
Thanks for providing the feedback and direction
-
Well, when using Qt, the first object you have to create is QCoreApplication or one of its subclass. It is needed in order to setup only the internals and plumbing needed for Qt to work.
1/12