diff --git a/src/utilities/sql/SqlFile_Impl.cpp b/src/utilities/sql/SqlFile_Impl.cpp index 8c49995a34..d32500e5df 100644 --- a/src/utilities/sql/SqlFile_Impl.cpp +++ b/src/utilities/sql/SqlFile_Impl.cpp @@ -71,7 +71,8 @@ namespace detail { initschema = true; } - sqlite3_open_v2(fileName.c_str(), &m_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE, nullptr); + int code = sqlite3_open_v2(fileName.c_str(), &m_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE, nullptr); + m_connectionOpen = (code == 0); if (initschema) { execAndThrowOnError( diff --git a/src/utilities/sql/SqlFile_Impl.hpp b/src/utilities/sql/SqlFile_Impl.hpp index dceabf9fad..4b7a7a3693 100644 --- a/src/utilities/sql/SqlFile_Impl.hpp +++ b/src/utilities/sql/SqlFile_Impl.hpp @@ -1306,11 +1306,11 @@ namespace detail { // Variadic arguments are the bind arguments if any, to replace '?' placeholders in the statement string template void execAndThrowOnError(const std::string& bindingStatement, Args&&... args) { - if (m_db) { - PreparedStatement stmt(bindingStatement, m_db, false, args...); - stmt.execAndThrowOnError(); + if (!m_connectionOpen) { + throw std::runtime_error("Error executing SQL statement as database connection is not open."); } - throw std::runtime_error("Error executing SQL statement as database connection is not open."); + PreparedStatement stmt(bindingStatement, m_db, false, args...); + stmt.execAndThrowOnError(); } void addSimulation(const openstudio::EpwFile& t_epwFile, const openstudio::DateTime& t_simulationTime, const openstudio::Calendar& t_calendar);