我的所有数据库操作使用单例模式来进行,以下是单例的部分代码:
#ifndef DATABASETHREAD_H
#define DATABASETHREAD_H
#include <QThread>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include "myquerymodel.h"
#include <QSqlError>
#include <QVariant>
#include <QDate>
#include "ParamStruct.h"
#include <QMutexLocker>
class DatabaseThread : public QThread
{
Q_OBJECT
public:
explicit DatabaseThread(QObject *parent = nullptr);
static DatabaseThread *Instance(){
if(!s_DatabaseThread){
QMutexLocker locker(&mutex);
if(!s_DatabaseThread){
s_DatabaseThread = new DatabaseThread;
}
}
return s_DatabaseThread;
}
static void deleteInstance(){
if(s_DatabaseThread){
s_DatabaseThread->quit();
s_DatabaseThread->wait();
s_DatabaseThread->deleteLater();
s_DatabaseThread = Q_NULLPTR;
}
}
QStringList getNuclideList();
QStringList getAlphaList();
QStringList getBetaList();
QString getNuclideType(QString name);
QStringList getSpecimenList();
QStringList getNoquenchlst();
QStringList getAlphaBetaList();
double getActive(QString name);
double getActive_curve(QString name);
void getActive_noquench(QString name,double &h,double &c);
void getActive_AB(QString name,double &alpha,double &beta);
bool addTask(TaskParam task);
QStringList getTaskList();
MyQueryModel* getNuclideModel();
TaskParam getTaskParam(QString name);
TaskParam getHisTask(QString taskid);
MyQueryModel* getNoquenchModel();
MyQueryModel* getQuenchModel();
MyQueryModel* getSpecimenModel();
MyQueryModel* getAlphaBetaModel();
bool delTask(QString name);
void getHalfLife(QString nuclide,float &half,QString &unit);
bool updateNuclide(QString name,QString half,QString unit,QString type,QString ener);
bool updateNuclide(QString name,QString half,QString unit,QString type,QString ener,
QString hv1,QString hv2,QString hv3,QString vts1,QString vts2,QString vts3,QString mag,QString tcr,QString tcr_);
bool addNuclide(QString name,QString half,QString unit,QString type,QString ener);
bool addNuclide(QString name,QString half,QString unit,QString type,QString ener,
QString hv1,QString hv2,QString hv3,QString vts1,QString vts2,QString vts3,QString mag,QString tcr,QString tcr_);
bool delNuclide(QString name);
QVector<quint16> getNuclideHv(QString name);
QVector<quint16> getOtherParam(QString name);
private: static DatabaseThread *s_DatabaseThread; static QMutex mutex; QSqlDatabase db; int command; MyQueryModel *nuclider_model; MyQueryModel *noquench_model; MyQueryModel *quench_model; MyQueryModel *specimen_model; MyQueryModel *alphabate_model; QSqlQueryModel *hisdata_model; QSqlQuery *taskqry;
void checkAndCreateTable();signals:
};
#endif // DATABASETHREAD_H
下面是cpp文件的部分代码:
#include "databasethread.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include "math.h"
DatabaseThread *DatabaseThread::s_DatabaseThread = 0;
QMutex DatabaseThread::mutex;
DatabaseThread::DatabaseThread(QObject *parent) : QThread(parent)
{
if(QSqlDatabase::contains("Data"))
db = QSqlDatabase::database("Data");
else
db = QSqlDatabase::addDatabase("QSQLITE","Data");
db.setDatabaseName("Liquidflash.db3");
db.open();
//checkAndCreateTable();
nuclider_model = new MyQueryModel(this);
noquench_model = new MyQueryModel(this);
quench_model = new MyQueryModel(this);
specimen_model = new MyQueryModel(this);
alphabate_model = new MyQueryModel(this);
hisdata_model = new QSqlQueryModel(this);
taskqry = new QSqlQuery(db);
}
我特意建了一个新的数据库,里面数据很少,但是仅使用了一天后,又出现了这种情况
整个数据库才1mb,想请教各位大佬,这种情况一般是怎么产生的?
#ifndef DATABASETHREAD_H
#define DATABASETHREAD_H
#include <QThread>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include "myquerymodel.h"
#include <QSqlError>
#include <QVariant>
#include <QDate>
#include "ParamStruct.h"
#include <QMutexLocker>
class DatabaseThread : public QThread
{
Q_OBJECT
public:
explicit DatabaseThread(QObject *parent = nullptr);
static DatabaseThread *Instance(){
if(!s_DatabaseThread){
QMutexLocker locker(&mutex);
if(!s_DatabaseThread){
s_DatabaseThread = new DatabaseThread;
}
}
return s_DatabaseThread;
}
static void deleteInstance(){
if(s_DatabaseThread){
s_DatabaseThread->quit();
s_DatabaseThread->wait();
s_DatabaseThread->deleteLater();
s_DatabaseThread = Q_NULLPTR;
}
}
QStringList getNuclideList();
QStringList getAlphaList();
QStringList getBetaList();
QString getNuclideType(QString name);
QStringList getSpecimenList();
QStringList getNoquenchlst();
QStringList getAlphaBetaList();
double getActive(QString name);
double getActive_curve(QString name);
void getActive_noquench(QString name,double &h,double &c);
void getActive_AB(QString name,double &alpha,double &beta);
bool addTask(TaskParam task);
QStringList getTaskList();
MyQueryModel* getNuclideModel();
TaskParam getTaskParam(QString name);
TaskParam getHisTask(QString taskid);
MyQueryModel* getNoquenchModel();
MyQueryModel* getQuenchModel();
MyQueryModel* getSpecimenModel();
MyQueryModel* getAlphaBetaModel();
bool delTask(QString name);
void getHalfLife(QString nuclide,float &half,QString &unit);
bool updateNuclide(QString name,QString half,QString unit,QString type,QString ener);
bool updateNuclide(QString name,QString half,QString unit,QString type,QString ener,
QString hv1,QString hv2,QString hv3,QString vts1,QString vts2,QString vts3,QString mag,QString tcr,QString tcr_);
bool addNuclide(QString name,QString half,QString unit,QString type,QString ener);
bool addNuclide(QString name,QString half,QString unit,QString type,QString ener,
QString hv1,QString hv2,QString hv3,QString vts1,QString vts2,QString vts3,QString mag,QString tcr,QString tcr_);
bool delNuclide(QString name);
QVector<quint16> getNuclideHv(QString name);
QVector<quint16> getOtherParam(QString name);
private: static DatabaseThread *s_DatabaseThread; static QMutex mutex; QSqlDatabase db; int command; MyQueryModel *nuclider_model; MyQueryModel *noquench_model; MyQueryModel *quench_model; MyQueryModel *specimen_model; MyQueryModel *alphabate_model; QSqlQueryModel *hisdata_model; QSqlQuery *taskqry;
void checkAndCreateTable();signals:
};
#endif // DATABASETHREAD_H
下面是cpp文件的部分代码:
#include "databasethread.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include "math.h"
DatabaseThread *DatabaseThread::s_DatabaseThread = 0;
QMutex DatabaseThread::mutex;
DatabaseThread::DatabaseThread(QObject *parent) : QThread(parent)
{
if(QSqlDatabase::contains("Data"))
db = QSqlDatabase::database("Data");
else
db = QSqlDatabase::addDatabase("QSQLITE","Data");
db.setDatabaseName("Liquidflash.db3");
db.open();
//checkAndCreateTable();
nuclider_model = new MyQueryModel(this);
noquench_model = new MyQueryModel(this);
quench_model = new MyQueryModel(this);
specimen_model = new MyQueryModel(this);
alphabate_model = new MyQueryModel(this);
hisdata_model = new QSqlQueryModel(this);
taskqry = new QSqlQuery(db);
}
我特意建了一个新的数据库,里面数据很少,但是仅使用了一天后,又出现了这种情况
整个数据库才1mb,想请教各位大佬,这种情况一般是怎么产生的?