diff --git a/xyylMCWEACSystem/SqlExecute.cpp b/xyylMCWEACSystem/SqlExecute.cpp new file mode 100644 index 0000000..52a3037 --- /dev/null +++ b/xyylMCWEACSystem/SqlExecute.cpp @@ -0,0 +1,124 @@ +#include "SqlExecute.h" +#include +#include +#include "SqlCore.h" +#include + +SqlExecute::SqlExecute(QObject * parent/*=NULL*/) +{ + +} + +SqlExecute::~SqlExecute() +{ + +} + +void SqlExecute::init(QString sDriver, QString sUser, QString sPasswd, QString sDBFile) +{ + SqlCore::init(sDriver, sUser, sPasswd, sDBFile); + SqlCore::getSqlDataBase(); +} + +bool SqlExecute::exeRead(QString sql, QList &list) +{ + bool ok; + QSharedPointer m_sqlQuery = SqlCore::select(sql, ok); + if (!ok) + { + //logInfo()<< QString::fromLocal8Bit("error:[%1]£¬Ö´ÐÐ[%2]").arg(m_sqlQuery->lastError().text()).arg(sql) << endl; + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return false; + } + while (m_sqlQuery->next()) + { + QVariantMap map; + for (int i = 0; i < m_sqlQuery->record().count(); i++) + { + map.insert(m_sqlQuery->record().field(i).name(), + m_sqlQuery->record().field(i).value()); + } + list.append(map); + } ; + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return true; +} + + +bool SqlExecute::exeRead(QString sql, QList> &list, QStringList sResuleSeq) +{ + bool ok; + QSharedPointer m_sqlQuery = SqlCore::select(sql, ok); + if (!ok) + { + //logInfo() << QString::fromLocal8Bit("error:[%1]£¬Ö´ÐÐ[%2]").arg(m_sqlQuery->lastError().text()).arg(sql) << endl; + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return false; + } + + while (m_sqlQuery->next()) + { + QList listdata; + for (int i = 0; i < m_sqlQuery->record().count(); i++) + { + int nameCol = m_sqlQuery->record().indexOf(sResuleSeq.at(i)); + listdata.append(m_sqlQuery->value(nameCol)); + } + list.append(listdata); + }; + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return true; +} + +bool SqlExecute::exeWrite(QString sql) +{ + bool ok; + QSharedPointer m_sqlQuery = SqlCore::select(sql, ok); + if (!ok) + { + //logInfo() << QString::fromLocal8Bit("error:[%1]£¬Ö´ÐÐ[%2]").arg(m_sqlQuery->lastError().text()).arg(sql) << endl; + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return false; + } + m_sqlQuery->finish(); + m_sqlQuery->clear(); + return true; +} + +void SqlExecute::destroyConn() +{ + SqlCore::destroyConn(); +} + +QSqlDatabase SqlExecute::getDataBase() +{ + + return SqlCore::getSqlDataBase(); + +} + +QList SqlExecute::getValues(QSharedPointer sqlQuery, int page, int pageNum) +{ + QList list; + if (!sqlQuery->seek(page)) + { + return list; + } + do + { + QVariantMap map; + for (int i = 0; i < sqlQuery->record().count(); i++) + { + map.insert(sqlQuery->record().field(i).name(), + sqlQuery->record().field(i).value()); + } + list.append(map); + } while (sqlQuery->next() && --pageNum); + return list; +} + diff --git a/xyylMCWEACSystem/SqlExecute.h b/xyylMCWEACSystem/SqlExecute.h new file mode 100644 index 0000000..2e43f46 --- /dev/null +++ b/xyylMCWEACSystem/SqlExecute.h @@ -0,0 +1,37 @@ +#ifndef _SqlExecute_H__ +#define _SqlExecute_H__ +/* + \author: zym + \brief : + \version 1.0 + \note + \CopyRight:zym +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + class SqlExecute :public QObject + { + Q_OBJECT + public: + SqlExecute(QObject * parent = NULL); + virtual ~SqlExecute(); + static void init(QString sDriver, QString sUser, QString sPasswd, QString sDBFile); + static bool exeRead(QString sql, QList &list); + static bool exeRead(QString sql, QList> &list, QStringList sResuleSeq); + static bool exeWrite(QString sql); + static void destroyConn(); + static QSqlDatabase getDataBase(); + private: + bool saveSql(QString sqlAction); + QList getValues(QSharedPointer sqlQuery, int page, int pageNum); + }; + +#endif // SqlExecute_h__