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. QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work.
Forum Update on Monday, May 27th 2025

QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.7k Views
  • 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.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    I have a folder where i have a many databases. Some times may be deleted or added database to the folder.
    So I use QTimer and read all databases.

    It is a my code:

    this->timer = new QTimer(this);
    this->timer->setInterval(15000);
    connect(this->timer, &QTimer::timeout, this, [=]() {
        QString path = "C:\\Users\\User\\Desktop\\DAXI SMS SENDER\\SMSSenderAllBASE";
        //QString path = qApp->applicationDirPath() + "\\SMSSenderAllBASE";
        QDir recoredDir(path);
        QStringList allFiles = recoredDir.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
        for (int i = 0; i < allFiles.size(); i++) {
            QString fullPath = path + "\\" + allFiles[i];
            QString connectionName = allFiles[i];
            connectionName = connectionName.remove(connectionName.size() - 4, 4);
            QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
            db.setDatabaseName(fullPath);
            db.setHostName("localhost");
            db.setPort(3050);
            db.setUserName("SYSDBA");
            db.setPassword("masterkey");
    
            thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
            connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
                QSqlDatabase t_db = QSqlDatabase::database(connectionName);
                t_db.close();
                QSqlDatabase::removeDatabase(connectionName);
                delete help;
            });
            help->run();
        }
    });
    this->timer->start();
    

    Yes I'm sure that the helperFinished signal will happen and this time I will not have any connection with this base.

    EDIT:
    If i remove

       thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this);
       connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){
           QSqlDatabase t_db = QSqlDatabase::database(connectionName);
           t_db.close();
           QSqlDatabase::removeDatabase(connectionName);
           delete help;
       });
       help->run();
    

    example:

    for (int i = 0; i < allFiles.size(); i++) {
        QString fullPath = path + "\\" + allFiles[i];
        QString connectionName = allFiles[i];
        connectionName = connectionName.remove(connectionName.size() - 4, 4);
        QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName);
        db.setDatabaseName(fullPath);
        db.setHostName("localhost");
        db.setPort(3050);
        db.setUserName("SYSDBA");
        db.setPassword("masterkey");
    
        QSqlDatabase::removeDatabase(connectionName);
    }
    

    I have the same error.

    Do what you want.

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Take a look at the removeDatabase documentation for an example of how to implement it properly.

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #3

      @SGaist
      Yes i using it wrong.

      Wrong use

      QSqlDatabase db = QSqlDatabase::database("sales");
      QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
      QSqlDatabase::removeDatabase("sales"); // will output a warning
      

      Correct use

      {
          QSqlDatabase db = QSqlDatabase::database("sales");
          QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
      }
      // Both "db" and "query" are destroyed because they are out of scope
      QSqlDatabase::removeDatabase("sales"); // correct
      

      Do what you want.

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

        Hi,

        Take a look at the removeDatabase documentation for an example of how to implement it properly.

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

        Taz742T 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          Take a look at the removeDatabase documentation for an example of how to implement it properly.

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #3

          @SGaist
          Yes i using it wrong.

          Wrong use

          QSqlDatabase db = QSqlDatabase::database("sales");
          QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
          QSqlDatabase::removeDatabase("sales"); // will output a warning
          

          Correct use

          {
              QSqlDatabase db = QSqlDatabase::database("sales");
              QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
          }
          // Both "db" and "query" are destroyed because they are out of scope
          QSqlDatabase::removeDatabase("sales"); // correct
          

          Do what you want.

          1 Reply Last reply
          2

          • Login

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