我第二次尝试连接数据库时收到此消息:
QSqlDatabasePrivate::removeDatabase: connection 'CompanyDatabase' is still in use, all queries will cease to work.
我删除了之前使用的模型,删除了所有连接,并关闭了数据库。下面我已经包含了打开CompanyWidget作为模式对话框的小部件,因为我认为我打开/关闭使用数据库的小部件可能是相关的。任何人解释为什么这个数据库仍然在使用?
编辑:如果我将QSqlDatabase成员更改为指针并使用以下命令创建它:
this->CompanyDatabase = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "CompanyDatabase"));
并在重新添加数据库之前将其删除:
if(this->CompanyDatabase)
{
delete this->CompanyDatabase;
this->CompanyDatabase = NULL;
}
然后错误就消失了。为什么这与我使用非指针成员的方式有什么不同?
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;
}
答案 0 :(得分:0)
文档明确说明:http://doc.qt.io/qt-4.8/qsqldatabase.html#removeDatabase。在删除连接之前,必须销毁已实例化的所有查询对象和QSqlDatabase连接对象。当你删除指针时,你实际上是这样做的。使用之前的代码,你不是。