1、自己设计的一个sqlITE 管理类
头文件
#ifndef AEYE_PTC_sqlITE
#define AEYE_PTC_sqlITE
#include "qobject.h"
#include <Qtsql/QsqlQuery>
#include <QDebug>
#include <Qtsql/QsqlError>
#include <Qtsql/QsqlDatabase>
#include <qmap.h>
#include "Def_Struct.h"
#include <QDateTime>
#include <QMutex>
#include <QVector>
#include "PTCCommunication.h"
#ifdef _DEBUG
#pragma comment(lib,"Qtsqld4.lib")
#else
#pragma comment(lib,"Qtsql4.lib")
#endif
#define AYAsqlITE sqliteManger::getInstance()
class sqliteManger : public QObject
{
Q_OBJECT
private:
explicit sqliteManger(QObject *parent = 0);
public:
static sqliteManger * getInstance()
{
if (m_InstanceSQ == NULL)
m_InstanceSQ = new sqliteManger();
return m_InstanceSQ;
}
static void realseInstance()
{
if(m_InstanceSQ!=NULL)
{
delete m_InstanceSQ;
m_InstanceSQ=NULL;
}
}
private:
static sqliteManger * m_InstanceSQ;
QString m_strDataBaceName;
QMutex m_QMutex; // 互斥量
//QsqlQuery query;
public:
//bool Insertsql(QMap<QString,QString> &mapSS,QMap<QString,int> &mapSI,QString _table);
bool Updatasql(QMap<QString,QString> mapSS,int> mapSI,QString _table,QString strConditions);
int SelectMaxId(QString _table);
int SelectCountItems(QString _table);
bool Deletesql(bool isAll,int iID,QString _table);
//T_PersonBaceInfor
bool InsertBaceInfor(StructCheckMBRT baceinfor,int isUpdata);
void SelectBaceInfor(int index,QVector<int>& arrINT,QMap<int,int>& mapUp);
//T_MODES
bool InsertModes(StruModel modeInfor,int isUpdata);
bool SelectModes(int index,int>& mapUp);
//t_num_..
bool InsertAddr(QString upcode,QString selfcode,QString desc,QString _tableName);
bool SelectAddr(QVector<StruAddrList>& arrAddr,QString _tableName);
//t_login
bool login(QString username,QString password);
bool UpdataFailNum(QString TableName,int updatanum,int id);
};
#endif
.Cpp 文件
int sqliteManger::SelectCountItems(QString _table)
{
QMutexLocker lock(&m_QMutex);
QString strsql;
strsql = QString("select count(*) max from %1;").arg(_table);
QsqlDatabase database;
if(QsqlDatabase::contains("GBond"))
database = QsqlDatabase::database("GBond");
else
{
database = QsqlDatabase::addDatabase("QsqlITE","GBond");
database.setDatabaseName("../Resources/db/TYCL.db");
}
if(!database.open())
{
database.close();
return false;
}
{
QsqlQuery query;
int ret = 0;
query = QsqlQuery(database);
if (!query.exec(strsql))
{
QsqlError error = query.lastError();
qDebug()<<error.databaseText();
QsqlDatabase::removeDatabase("QsqlITE");
database.close();
return -1;
}
while (query.next())
{
ret = query.value(0).toInt();
QsqlDatabase::removeDatabase("QsqlITE");
database.close();
return ret;
}
}
database.close();
QsqlDatabase::removeDatabase("QsqlITE");
return -1;
}
bool sqliteManger::Deletesql(bool isAll,QString _table)
{
QMutexLocker lock(&m_QMutex);
QsqlDatabase database;
if(QsqlDatabase::contains("GBond"))
database = QsqlDatabase::database("GBond");
else
{
database = QsqlDatabase::addDatabase("QsqlITE","GBond");
database.setDatabaseName("../Resources/db/TYCL.db");
}
if(!database.open())
{
database.close();
return false;
}
QString strsql;
if (isAll)
{
strsql = QString("delete from %1 where id = %2")
.arg(_table)
.arg(iID);
}
else
{
strsql = QString("delete from %1").arg(_table);
}
{
QsqlQuery query;
query = QsqlQuery(database);
if (!query.exec(strsql))
{
QsqlError error = query.lastError();
qDebug()<<error.databaseText();
database.close();
QsqlDatabase::removeDatabase("QsqlITE");
return false;
}
}
database.close();
QsqlDatabase::removeDatabase("QsqlITE");
return true;
}