Open database outside of multi-settings loop to avoid reconnection warnings

This commit is contained in:
Bill Somerville
2018-11-12 01:46:16 +00:00
parent 0b03ad4c09
commit ff7c2743b4
2 changed files with 23 additions and 22 deletions
+23
View File
@@ -20,6 +20,8 @@
#include <QSplashScreen>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QSqlDatabase>
#include <QSqlError>
#include "revision_utils.hpp"
#include "MetaDataRegistry.hpp"
@@ -233,6 +235,27 @@ int main(int argc, char *argv[])
}
}
// create writeable data directory if not already there
auto writeable_data_dir = QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)};
if (!writeable_data_dir.mkpath ("."))
{
MessageBox::critical_message (nullptr, a.translate ("main", "Failed to create data directory"),
a.translate ("main", "path: \"%1\"").arg (writeable_data_dir.absolutePath ()));
throw std::runtime_error {"Failed to create data directory"};
}
// set up SQLite database
if (!QSqlDatabase::drivers ().contains ("QSQLITE"))
{
throw std::runtime_error {"Failed to find SQLite Qt driver"};
}
auto db = QSqlDatabase::addDatabase ("QSQLITE");
db.setDatabaseName (writeable_data_dir.absoluteFilePath ("db.sqlite"));
if (!db.open ())
{
throw std::runtime_error {("Database Error: " + db.lastError ().text ()).toStdString ()};
}
int result;
do
{