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

QSqlDatabasePrivate::removeDatabase: connection 'cbsdb' is still in use.



  • hi to all, I am building a small project. I've centos 6.10 server of mysql, and centos 7.5 as client of Qt 5.7.
    here i created login form( DialogLogin ),
    here is mine dialoglogin.h:-

    #ifndef DIALOGLOGIN_H
    #define DIALOGLOGIN_H
    
    #include <QDialog>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QMessageBox>
    #include <QString>
    #include <QSqlError>
    
    #include "dialogmenu.h"
    
    class DialogMenu;
    
    namespace Ui {
    class DialogLogin;
    }
    
    class DialogLogin : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit DialogLogin(QWidget *parent = 0);
        ~DialogLogin();
    
        QSqlDatabase *db;
    
    private slots:
    
    
        void on_pushButtonCancel_clicked();
    
        void on_pushButtonLogin_clicked();
    
    private:
        Ui::DialogLogin *ui;
    //    QSqlDatabase *db;
        QString sql;
        QSqlQuery *query;
    
        DialogMenu *dlgmenu;
    };
    
    #endif // DIALOGLOGIN_H
    

    and dialoglogin.cpp :-

    #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);
    
        dlgmenu = new DialogMenu(this);
        query = new QSqlQuery;
    }
    
    DialogLogin::~DialogLogin()
    {
    
        db->removeDatabase("cbsdb");
        db->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 = ?";
    
    //        query = new QSqlQuery(sql);
    
            QString user = ui->lineEditUserName->text().trimmed();
            QString password = ui->lineEditPassword->text().trimmed();
    //        sql = "select user from tableLogin where User = '"+user +"' and Password = '"+password+"';";
            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) )
                    {
                        //QMessageBox::information(this, "Login Form", "Login Correct");
                        this->close();
                        dlgmenu->exec();
                        db->close();
                    }
                    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());
        }
    
        db->removeDatabase("cbsdb");
    }
    

    i added login->db->open() and login->db->close() in every function where i used database connection.e.g.

    void DialogAddModifyMemberRecords::on_pushButtonDeleteRecord_clicked()
    {
        if(login->db->open())
        {
            QString memstr = ui->lineEditMembershipNumber->text().trimmed();
            sql = "delete from tableMemberRecords where membershipno = '"+memstr+"'";
            qry->prepare(sql);
            if(qry->exec(sql) && !memstr.isEmpty())
            {
                QMessageBox::information(this, "deleting from member record", "record sucesfully deleted");
            }
            else
            {
                QMessageBox::critical(this, "deleting from member record", "Error " + qry->lastError().text());
            }
        }
        login->db->close();
    }
    

    now i am getting this error :-
    QSqlDatabasePrivate::removeDatabase: connection 'cbsdb' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'cbsdb', old connection removed.

    how to get rid of this.


  • Lifetime Qt Champion

    Hi,

    You are using QSqlDatabase wrongly.

    Please read again the documentation of QSqlDatabase and especially the warning about not keeping a local copy of it.


  • Lifetime Qt Champion

    And apart from what @SGaist said, your order is wrong. First close the database, then remove the database connection

    DialogLogin::~DialogLogin()
    {
        QSqlDataBase::database("cdsdb").close();
        QSQLDataBase::removeDatabase("cbsdb");
        delete ui;
    }
    

Log in to reply