Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabase known connection issue: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase known connection issue: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

Scheduled Pinned Locked Moved General and Desktop
qsqldatabase
6 Posts 3 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JorgeJ Offline
    JorgeJ Offline
    Jorge
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Am I reading correctly that you recreate the default connection in each widget ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JorgeJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Am I reading correctly that you recreate the default connection in each widget ?

        JorgeJ Offline
        JorgeJ Offline
        Jorge
        wrote on last edited by
        #3

        @SGaist Apparently, I do so. You see, I was running fine this application about a month ago, but ever since I added the 5.5.0 MinGW kit this problem showed up. I suppose the default connection is the problem... Is it?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So with 5.4.2 no warning and with 5.5 you have it ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            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.

            JorgeJ 1 Reply Last reply
            0
            • C ChrisW67

              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.

              JorgeJ Offline
              JorgeJ Offline
              Jorge
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved