#include "HxSql.h" #include "HxTrace.h" #include #include #include #include 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); }