Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:45:46+00:00 2026-05-27T13:45:46+00:00

The second time I try to connect to a database, I get this message:

  • 0

The second time I try to connect to a database, I get this message:

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

I delete the Model that I had previously used, I delete all connections, and I close the database. Below I have included the widget that opens the CompanyWidget as a modal dialog, because I think it might be relevant how I am opening/closing the widget which uses the database. Cany anyone explain why this database is still in use?

EDIT: If I change the QSqlDatabase member to a pointer and create it using:

this->CompanyDatabase = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "CompanyDatabase"));

and delete it before re-adding the database:

  if(this->CompanyDatabase)
    {
    delete this->CompanyDatabase;
    this->CompanyDatabase = NULL;
    }

then the error goes away. Why is this any different then the way I was doing it with the non-pointer member?

EditCompanyWidget.h

#ifndef EditCompanyWidget_H
#define EditCompanyWidget_H

#include "ui_EditCompanyWidget.h"

#include <QDialog>
#include <QSqlDatabase>

class QSqlTableModel;


class EditCompanyWidget : public QDialog, private Ui::EditCompanyWidget
{
Q_OBJECT
public:

  EditCompanyWidget(QSqlDatabase);
  ~EditCompanyWidget();

public slots:

  void on_btnExit_clicked();

protected:
  QSqlTableModel* Model;
};

#endif

EditCompanyWidget.cpp

#include "EditCompanyWidget.h"

#include <iostream>

#include <QSqlDatabase>
#include <QStringList>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QVariant>
#include <QSqlTableModel>
#include <QSqlRecord>

EditCompanyWidget::EditCompanyWidget(QSqlDatabase database) : QDialog(NULL)
{
  setupUi(this);

  //QSqlTableModel* model = new QSqlTableModel;
  this->Model = new QSqlTableModel(NULL, database);
  this->Model->setTable("CompanyTable");
  this->Model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  //model->setFilter(QString("id=%1").arg(1));
  this->Model->select();

  this->tableView->setModel(this->Model);
}

EditCompanyWidget::~EditCompanyWidget()
{
  if(this->Model)
    {
    std::cout << "destructor Deleting..." << std::endl;
    delete this->Model;
    this->Model = NULL;
    }
}


void EditCompanyWidget::on_btnExit_clicked()
{
  this->Model->revertAll();
  if(this->Model)
    {
    std::cout << "btnExit Deleting..." << std::endl;
    delete this->Model;
    this->Model = NULL;
    }
  //this->setResult(QDialog::Rejected);
  this->reject();
}

MainWidget.h

#ifndef TimeTrackerConfigurationWidget_H
#define TimeTrackerConfigurationWidget_H

#include "ui_TimeTrackerConfigurationWidget.h"

#include <QMainWindow>
#include <QSqlDatabase>

class TimeTrackerConfigurationWidget : public QMainWindow, private Ui::TimeTrackerConfigurationWidget
{
Q_OBJECT
public:
  TimeTrackerConfigurationWidget(QWidget *parent = 0);

public slots:
  void on_btnNewCompany_clicked();
  void on_btnEditCompany_clicked();

protected:
  QSqlDatabase CompanyDatabase;
};

#endif

MainWidget.cpp

#include "TimeTrackerConfigurationWidget.h"

#include "EditCompanyWidget.h"

#include <iostream>

#include <QSqlDatabase>
#include <QFileDialog>
#include <QSqlError>
#include <QSqlQuery>

TimeTrackerConfigurationWidget::TimeTrackerConfigurationWidget(QWidget *parent) : QMainWindow(parent)
{
  setupUi(this);
}


void TimeTrackerConfigurationWidget::on_btnEditCompany_clicked()
{
//   QString fileName = QFileDialog::getOpenFileName(this, "Database File", ".", "Image Files (*.sqlite)");
//
//   if(fileName.toStdString().empty())
//     {
//     std::cout << "Filename was empty." << std::endl;
//     return;
//     }

  QString fileName = "test.sqlite";

  if(!QFile::exists(fileName))
    {
    std::cerr << "File does not exist!" << std::endl;
    return;
    }

  this->CompanyDatabase.close();
  //QSqlDatabase::removeDatabase("CompanyDatabase");
  //QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");

  // Delete all connections
  for(unsigned int i = 0; i < QSqlDatabase::connectionNames().size(); ++i)
    {
    //QSqlDatabase::removeDatabase("TestConnection");
    QSqlDatabase::removeDatabase(QSqlDatabase::connectionNames()[i]);
    }


  this->CompanyDatabase = QSqlDatabase::addDatabase("QSQLITE", "CompanyDatabase");
  //this->CompanyDatabase = QSqlDatabase::addDatabase("QSQLITE");
  this->CompanyDatabase.setDatabaseName(fileName);
  if (!this->CompanyDatabase.open())
    {
    std::cerr << "Could not open database" << std::endl;
    std::cerr << this->CompanyDatabase.lastError().text().toStdString() << std::endl;
    return;
    }

  EditCompanyWidget* editCompanyWidget = new EditCompanyWidget(this->CompanyDatabase);
  editCompanyWidget->exec();
  delete editCompanyWidget;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:45:46+00:00Added an answer on May 27, 2026 at 1:45 pm

    The documentation says it clearly: http://doc.qt.io/qt-4.8/qsqldatabase.html#removeDatabase. You have to destroy all the queries objects and the QSqlDatabase connection objects you have instantiated before removing the connection. When you delete the pointer, you’re actually doing this. With your previous code, you weren’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my first time is 12:10:20 PM and second time is 7:10:20 Am of the
How do I disable a textbox the second time? here is my code, In
I just got done working through the Django tutorials for the second time, and
How do I prevent a form that is posted a second time because of
I am not getting why the colon shifted left in the second time >>>
When I make the same query twice, the second time it does not return
The system time function time(0) gives me a resolution of 1 second, right? Is
Given a date/time as an array of (year, month, day, hour, minute, second), how
The function code: # Connect to the DB try: dbi = MySQLdb.connect(host='localhost', \ user='user',
I'm trying to get the status from facebook users, who connect to my app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.