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

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



  • hi to all, I've qt 5.7 in centos7. I am facing problem of connection "'qt_sql_default_connection' is still in use, all queries will cease to work." When i start program it errors like :

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    

    mine starting code is :-

    #include "dialoglogin.h"
    #include "ui_dialoglogin.h"
    
    DialogLogin::DialogLogin(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogLogin)
    {
        ui->setupUi(this);
        //QString database = "cbsdb";
        db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));//, "cbsdb"));
        db->setDatabaseName("cbs");
        db->setHostName("serverora.db.net");
        db->setUserName("rahul");
        db->setPassword("rahul");
        db->setPort(3306);
        db->setConnectOptions();
        dlgmenu = new DialogMenu(this);
        query = new QSqlQuery;
    }
    
    DialogLogin::~DialogLogin()
    {
        QSqlDatabase::database().close();
        delete db;
        delete ui;
    }
    
    void DialogLogin::on_pushButtonCancel_clicked()
    {
    
        exit(0);
    }
    
    void DialogLogin::on_pushButtonLogin_clicked()
    {
        if(db->open())
        {
            QMessageBox::information(this, "inside DialogLogin::db->open ","inside db->open");
            sql = "select User from tableLogin where User = ? and Password = ?";
    
            QString user = ui->lineEditUserName->text().trimmed();
            QString password = ui->lineEditPassword->text().trimmed();
            if(!query->prepare(sql))
            {
                QMessageBox::information(this, "inside DialogLogin::query->prepare", "Error : " + query->lastError().text());
                return;
            }
    
            query->bindValue(0, user);
            query->bindValue(1, password);
    
            if(query->exec())
            {
                if(query->next())
                {
                    if(QString::compare(user, query->value(0).toString().trimmed()) == 0) // && ( QString::compare(password, query->value(1).toString().trimmed()) == 0) )
                    {
                        this->close();
                        dlgmenu->exec();                    
                    }
                    else
                    {
                        QMessageBox::information(this, "Login Form", "user name or password is incorrect");
                    }
                }
                else
                {
                    QMessageBox::information(this, "Login Form", "query.next() is failed : " + query->lastError().text());
                }
            }
            else
            {
                QMessageBox::information(this, "Login Form", " Error in login " + query->lastError().text());
            }
        }
        else
        {
            QMessageBox::information(this,"cbs LoginForm", "database cannot be openned : " + db->lastError().text());
        }
    }
    

    error is :-


  • Lifetime Qt Champion

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

    db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));//, "cbsdb"));

    Why do you have this db variable? Please check documentation to see how to properly use https://doc.qt.io/qt-5/qsqldatabase.html
    Also, it does not make sense to create a new connection and then second one and use the first one as parameter! You're calling copy constructor this way...



  • because db is pointer so i have to do this
    db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));

    after that i use this to connect database with front end.


  • Lifetime Qt Champion

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

    so i have to do this

    No, you don't!
    Please read documentation!
    There is no need to create a QSqlDatabase instance manually - that is what addDatabase() does for you...



  • can you please tell me by example how to do it. Actually i have another mini project that accepts this.


  • Lifetime Qt Champion

    @rahulvishwakarma There is an example in the documentation I pointed you to



  • @rahulvishwakarma
    As @jsulm pointed you to, the very first example at https://doc.qt.io/qt-5/qsqldatabase.html#details shows you what you are supposed to do.


Log in to reply