HxUtils/HxSql.cpp
2024-01-11 16:14:21 +08:00

39 lines
963 B
C++

#include "HxSql.h"
#include "HxTrace.h"
#include <QDateTime>
#include <QSqlQuery>
#include <QVariant>
#include <QStorageInfo>
QSqlDatabase HxSql::open(QString filepath, QString connectionName)
{
QSqlDatabase database;
if (QSqlDatabase::contains(connectionName))
database = QSqlDatabase::database(connectionName);
else
database = QSqlDatabase::addDatabase("QSQLITE", connectionName);
database.setDatabaseName(filepath);
if (!database.open())
{
QString bk_filepath = filepath + QString(".[%1].bk").arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
QFile::copy(filepath, bk_filepath);
QFile::remove(filepath);
HxTrace::debug_write_line("database", QString("file backup success. %1 => %2").arg(filepath).arg(bk_filepath));
database.open();
}
return database;
}
void HxSql::close(QString connectionName)
{
QSqlDatabase::removeDatabase(connectionName);
}