QSqlDatabase known connection issue: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
-
Hello,
I'm getting the same issue I've read all over the internet about the default connection being still in use. I've tried using all of the solutions suggested from several forums, yet my problem is still present.
The bit of my code works fine the first time I open the Database, which is a simple login form. Note that the SQL database was declared in the header beforehand:#include "qlogin.h" #include "ui_qlogin.h" #include "mainwidget.h" #include <QDebug> #define Path_to_DB "C:/Qt/Tools/Projects/Skylord_III/LiquascanDB.sqlite.db" QLogin::QLogin(QWidget *parent) : QWidget(parent), ui(new Ui::QLogin) { ui->setupUi(this); iRole = 0; sqlLiquascanDB = QSqlDatabase::addDatabase("QSQLITE"); sqlLiquascanDB.setDatabaseName(Path_to_DB); QFileInfo checkFile(Path_to_DB); sConnectionName = sqlLiquascanDB.connectionName(); if(checkFile.isFile()) { if(sqlLiquascanDB.open()) { ui->lblStatus->setText("Conectado a base de datos."); qDebug() << "Connection established."; qDebug() << sConnectionName; } else { ui->lblStatus->setText("Sin conexión a base de datos"); qDebug() << "No connection."; } } } QLogin::~QLogin() { delete ui; sqlLiquascanDB = QSqlDatabase(); QSqlDatabase::removeDatabase(sConnectionName); }
I'd read that I could end the connection in the destroyer, which I tried. But then, the next time I try to access the database, which is in another widget, I get the titular error:
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
This is the code bit of the second widget class that tries to access the database:
#include "qdispense.h" #include "ui_qdispense.h" #include <QDebug> #include <QtSql> #define Path_to_DB "C:/Qt/Tools/Projects/Skylord_III/LiquascanDB.sqlite.db" QDispense::QDispense(QWidget *parent) : QWidget(parent), ui(new Ui::QDispense) { ui->setupUi(this); bBegin = false; sqlLiquascanDB = QSqlDatabase::addDatabase("QSQLITE"); sqlLiquascanDB.setDatabaseName(Path_to_DB); QFileInfo checkFile(Path_to_DB); if(checkFile.isFile()) { if(sqlLiquascanDB.open()) { ui->lblStatus->setText("Conectado a base de datos."); qDebug() << "Connection established."; } else { ui->lblStatus->setText("Sin conexión a base de datos."); qDebug() << "No connection."; } } connect(ui->btnBegin, SLOT(clicked()), this, SIGNAL(beginOperation(bool))); }
I still can't figure out the problem. Help would be greatly apprectiated. Thanks.
-
Hi,
Am I reading correctly that you recreate the default connection in each widget ?
-
So with 5.4.2 no warning and with 5.5 you have it ?
-
Your Login class constructor creates/defines the default Sql connection.
Your Dispense class constructor also creates/defines the default Sql connection.
Whichever object is constructed second will generate this warning because the default connection already exists.You should create the database connection once, outside either class would be good, and simply use the connection in the other classes. Do not hold a QSqlDatabase instance anywhere (i.e. sqlLiquascanDB looks like a persistent member variable), fetch it when (if) needed.
int main(...) { ... QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(Path_to_DB); // check here if it can be opened if you wish ... }
Where you need to use it:
QSqlDatabase db = QSqlDatabase::database(); if (db.isOpen()) { ... } // or, more simply if you just going to query on the default connection QSqlQuery qry("select some, cool, stuff from somewhere");
If you actually want two independent connections to the same Sqlite file then at least one must be a named connection. You name a connection in the addDatabase() call.
-
@ChrisW67 Thank you. I kind of understand what you mean, but recently I tried a solution that appears to work.
I have a class named mainWidget, in which the remaining widgets are instantiated. I simply retrieve the db connection name, store it in a variable and then whenever I switch widgets and need to re-open the database, I close said connection and addDatabase again. I am guessing this is not optimal, but it allows me to move on with the SQLite databases.