Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

Please do yourself a favor and never use conn = sqlite3.connect(sql_path), as if you try to rerun your simulation while the connection to the sql database is still open in python, it won't be able to write to the SQL file, which is very annoying (you could not realize that you aren't loading the results from your latest simulation). Instead use an URI setting the mode to read-only.

Here's a safer example:

In [1]:
import os
import re
import pathlib
import sqlite3
import pandas as pd

# Turn it into an absolute path
abs_sql_path = os.path.abspath(sql_path)
# Create a read-only URI
sql_uri = '{}?mode=ro'.format(pathlib.Path(abs_sql_path).as_uri())

A good idea is probably to start by checking simulation info, to ensure you have what you need

In [2]:
query = 'SELECT EnergyPlusVersion FROM Simulations'
with sqlite3.connect(sql_uri, uri=True) as con:
    cursor = con.cursor()
    r = cursor.execute(query).fetchone()
    if r:
        simulation_info = r[0]
    else:
        msg = ("Cannot find the EnergyPlusVersion in the SQL file. "
               "Please inspect query used:\n{}".format(query))
        raise ValueError(msg)

print(simulation_info)

Out[2]: EnergyPlus, Version 9.1.0-a310a424e4, YMD=2019.03.08 12:31

Then do stuff, favoring with statements too to ensure you close connections anyways

In [3]: 
with sqlite3.connect(sql_uri, uri=True) as con:
    df_env = pd.read_sql('SELECT * FROM EnvironmentPeriods', con=con)
df_env

Out[3]: 
   EnvironmentPeriodIndex  SimulationIndex                     EnvironmentName  EnvironmentType
0                       1                1     CHICAGO ANN HTG 99.6% CONDNS DB                1
1                       2                1  CHICAGO ANN CLG .4% CONDNS WB=>MDB                1