Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • I have this problem .
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

    can you help ?

    #include "testmanagerdal.h"
    
    testManagerDAL::testManagerDAL()
    {
        #ifdef Q_OS_WIN
        if (!QDir("Tests\\").exists())
        {
            QDir().mkdir("Tests\\") ;
        }
        #endif
    }
    QSqlQueryModel* testManagerDAL::getList()
    {
        QString filePath = "Tests\\demo.db";
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(filePath);
        if(!db.open())
        {
            qDebug()<<"cant open";
            return nullptr;
        }
        else
        {
            QSqlQueryModel *model = new QSqlQueryModel();
            QSqlQuery *qry= new QSqlQuery();
            qry->prepare("select name,startDate,endDate from demo");
            qry->exec();
            model->setQuery(*qry);
    
    
            return model;
    
            //db.removeDatabase(filePath);
        }
    }
    
    QString testManagerDAL::selectFromList(int index)
    {
    
        QString csvPath;
        QString filePath = "Tests\\demo.db";
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        QString connName= db.connectionName();
    
        db.setDatabaseName(filePath);
    
        if(!db.open())
        {
            qDebug()<<"cant open";
            return nullptr;
        }
        else
        {
            QSqlQuery query = *new QSqlQuery();
    
            int j=0;
            if(query.exec("Select filePath From ( Select Row_Number() Over (Order By id) As RowNum , * From demo ) demo Where RowNum = "+QString::number(index+1)))
            {
                while (query.next())
                {
                    csvPath = query.value(j).toString();
                    j++;
                }
            }
    
    
    
            return csvPath;
    
    
        }
    
    }
    
    QSqlQueryModel* testManagerDAL::insertTestIntoList(QString name,QString startDate,QString filePath,QString endDate)
    {
    
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("Tests\\demo.db");
        QString connName= db.connectionName();
        if(!db.open())
        {
            qDebug()<< "cant open";
            return nullptr;
        }
        else
        {
            QSqlQuery *query= new QSqlQuery(db);
            query->prepare("INSERT INTO demo (name, startDate,filePath,endDate) VALUES (?, ?, ?,?)");
            query->addBindValue(name);
            query->addBindValue(startDate);
            query->addBindValue(filePath);
            query->addBindValue(endDate);
            query->exec();
            QSqlQueryModel *model = new QSqlQueryModel();
            query->prepare("select name,startDate,endDate from demo");
            query->exec();
            model->setQuery(*query);
    
            return model;
        }
    
    }
    
    bool testManagerDAL::updateTest(QString name,QString endDate)
    {
    
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("Tests\\demo.db");
        QString connName= db.connectionName();
        if(!db.isOpen())
        {
            qDebug()<< "cant open";
            return false;
        }
        else
        {
            QSqlQuery *query= new QSqlQuery(db);
            query->prepare("UPDATE demo SET endDate = " +endDate+ " WHERE name =" +name);
            query->exec();
            QSqlQueryModel *model = new QSqlQueryModel();
            query->prepare("select name,startDate,endDate from demo");
            query->exec();
            model->setQuery(*query);
    
            return true;
        }
    
    }
    
    

  • Lifetime Qt Champion

    @V0rtex Why do you add database in all these methods? You should do that only once and then get the db calling https://doc.qt.io/qt-5/qsqldatabase.html#database



  • @jsulm could you it for one of them for me i am new at sql.


  • Lifetime Qt Champion

    @V0rtex said in QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.:

    i am new at sql.

    It has nothing to do with SQL.
    Do this

    QString filePath = "Tests\\demo.db";
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(filePath);
    

    once instead of putting it in all the methods you posted. You could do it in testManagerDAL constructor for example.


Log in to reply