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.
84 lines
1.8 KiB
84 lines
1.8 KiB
#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(QString hostName,QString databaseName,QString userName,QString password,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()
|
|
{
|
|
// if(db.isOpen())
|
|
// db.close();
|
|
return true;
|
|
}
|
|
|
|
bool XSqlExcute::excuteSQL(QString sql,QString connectionName)
|
|
{
|
|
QSqlQuery q(QSqlDatabase::database(connectionName));
|
|
return q.exec(sql);
|
|
}
|
|
|
|
//查询
|
|
QSqlQuery XSqlExcute::query(QString sql,QString connectionName)
|
|
{
|
|
QSqlQuery q(QSqlDatabase::database(connectionName));
|
|
q.exec(sql);
|
|
return q;
|
|
}
|
|
|
|
QSqlRecord XSqlExcute::record(QString sql,QString connectionName)
|
|
{
|
|
QSqlQuery q(QSqlDatabase::database(connectionName));
|
|
q.exec(sql);
|
|
QSqlRecord r = q.record();
|
|
return r;
|
|
}
|
|
|
|
//事务
|
|
void XSqlExcute::transaction(QString connectionName)
|
|
{
|
|
QSqlDatabase::database(connectionName).transaction();
|
|
}
|
|
|
|
void XSqlExcute::commit(QString connectionName)
|
|
{
|
|
QSqlDatabase::database(connectionName).commit();
|
|
}
|
|
void XSqlExcute::rollback(QString connectionName)
|
|
{
|
|
QSqlDatabase::database(connectionName).rollback();
|
|
}
|