Getting QSqlError "Driver not loaded" when trying to use QSQLITE database
-
Hi all, this is my first post for asking for anything programming related, so please forgive me if I'm missing something.
I'm doing a project for school that requires CRUD functionality; however, none of the databases are working for QT.
Here's what I'm doing to connect to the database:
```QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "SQLITE"); QString dbName = "path/database.sqlite"; db.setDatabaseName(dbName); qDebug() << db.connectionName(); if (!db.open()) { qDebug() << "There was an error opening the file"; } else { qDebug() << QSqlDatabase::drivers(); qDebug() << "Database opened successfully!"; QSqlQuery q; q.exec("select * from login"); while(q.next()) { int id = q.value(0).toInt(); qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/ }
When doing this, I get the error :
QSqlQuery::exec: database not open There was an issue opening the database QSqlError("", "Driver not loaded", "Driver not loaded")
Can someone help with troubleshooting the drivers? I've tried the steps in the QT SQLITE DRIVER setup, but do not have the nmake command available for use in terminal. Read more here: https://doc.qt.io/qt-5/sql-driver.html#qsqlite.
SPECS:
Qt Creator 4.13.3
Based on Qt 5.15.2 (Clang 11.0 (Apple), 64 bit)
Built on Nov 13 2020 12:39:07
MAC OSX 11.0.1 Beta (20B5022a) -
-
Qt has sqlite plugin installed by default, you don't need to build your own.
And your db is successfully opened, but since you calladdDatabase
with a specifiedconnectionName
, so it is not the "default connection".
When there is no "default connection", it is required to specified the db when constructingQSqlQuery
.
So there're two solutions:- Specify the db when constructing
QSqlQuery
QSqlQuery q(db);
- Or make your db the "default connection" by leaving the
connectionName
unspecified. This is what I would prefer if you have only one db to manage.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- Specify the db when constructing
-
Hi Bonnie,
Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.
Right after opening the connection, I test to see if the database is open:
if (!db.open()) { qDebug() << "There was an error opening the file"; } else { qDebug() << QSqlDatabase::drivers(); qDebug() << "Database opened successfully!"; QSqlQuery q; q.exec("select * from login"); while(q.next()) { int id = q.value(0).toInt(); qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/ }
However, I get this from db.lastError();
QSqlError("", "Driver not loaded", "Driver not loaded") ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...
Thanks in advance!
-
@stewpid Set QT_DEBUG_PLUGINS before starting your app and check its output (https://doc.qt.io/qt-5/debug.html#environment-variables-recognized-by-qt).
-
@stewpid
I can't tell since you haven't shown your full code and full output.
What's the return value ofdb.open()
in your above code? Where do you calldb.lastError()
?
My guess is still: you might be operating on an invalid QSqlDatabase object. -
@Bonnie Hi Bonnie,
Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.
Right after opening the connection, I test to see if the database is open:
if (!db.open()) { qDebug() << "There was an error opening the file"; } else { qDebug() << QSqlDatabase::drivers(); qDebug() << "Database opened successfully!"; QSqlQuery q; q.exec("select * from login"); while(q.next()) { int id = q.value(0).toInt(); qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/ }
However, I get this from db.lastError();
QSqlError("", "Driver not loaded", "Driver not loaded")
("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...Thanks in advance!
-