1. 程式人生 > >QT+SQLITE

QT+SQLITE

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,QMap<QString,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,QVector<int>& arrINT,QMap<int,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,int iID,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;
}