You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/***
* 数据库操作类,单例模式
* 只进行sql的执行具体内容的解析业务侧进行
*/
#include "xsqlexcute.h"
XSqlExcute* XSqlExcute::g_pSqlExc = NULL;
QMutex XSqlExcute::m_mutex;
XSqlExcute::XSqlExcute(QObject *parent) : QObject(parent)
{
}
XSqlExcute* XSqlExcute::instance()
{
if (g_pSqlExc == NULL)
{
QMutexLocker locker(&m_mutex); // 加锁
if (g_pSqlExc == NULL)
{
g_pSqlExc = new XSqlExcute();
}
}
return g_pSqlExc;
}
bool XSqlExcute::openMysql(const QString& hostName,const QString& databaseName,const QString& userName,const QString& password,const QString& connectionName)
{
if(QSqlDatabase::contains(connectionName))
{
return true;
}
else
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL",connectionName);
db.setHostName(hostName);
db.setDatabaseName(databaseName);
db.setUserName(userName);
db.setPassword(password);
return db.open();
}
}
bool XSqlExcute::closeMysql(const QString& connectionName)
{
if(QSqlDatabase::contains(connectionName))
QSqlDatabase::removeDatabase(connectionName);
return true;
}
bool XSqlExcute::excuteSQL(const QString& sql,const QString& connectionName)
{
QSqlQuery q(QSqlDatabase::database(connectionName));
return q.exec(sql);
}
//查询
QSqlQuery XSqlExcute::query(const QString& sql,const QString& connectionName)
{
QSqlQuery q(QSqlDatabase::database(connectionName));
q.exec(sql);
return q;
}
QSqlRecord XSqlExcute::record(const QString& sql,const QString& connectionName)
{
QSqlQuery q(QSqlDatabase::database(connectionName));
q.exec(sql);
QSqlRecord r = q.record();
return r;
}
//事务
void XSqlExcute::transaction(const QString& connectionName)
{
QSqlDatabase::database(connectionName).transaction();
}
void XSqlExcute::commit(const QString& connectionName)
{
QSqlDatabase::database(connectionName).commit();
}
void XSqlExcute::rollback(const QString& connectionName)
{
QSqlDatabase::database(connectionName).rollback();
}