Qt SQL doesn't detect any driver
-
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.
-
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. -
Can you check the content of the sqldrivers folder in the plugins directory of that Qt version ?
-
@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?
-
@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.
-
@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.