QT+SQLITE

QT+SQLITE,第1张

概述1、自己设计的一个SQLITE 管理类 头文件 #ifndef AEYE_PTC_SQLITE#define AEYE_PTC_SQLITE#include "qobject.h"#include <QtSql/QSqlQuery>#include <QDebug>#include <QtSql/QSqlError>#include <QtSql/QSqlDatabase>#incl

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_OBJECTprivate:    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;}
总结

以上是内存溢出为你收集整理的QT+SQLITE全部内容,希望文章能够帮你解决QT+SQLITE所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1163439.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存