Does pandas need to close connection?

Question:

When using pandas “read_sql_query”, do I need to close the connection? Or should I use a “with” statement? Or can I just use the following and be good?

from sqlalchemy import create_engine
import pandas as pd

sql = """
    SELECT * FROM Table_Name;
    """
engine = create_engine('blah')

df = pd.read_sql_query(sql, engine)

print df.head()
Asked By: deez

||

Answers:

Looking at the source code, I can’t find a con.close() method on any SQL connection object, only the cursor objects for the queries.

I’d close for safe-measure. Whether you do that using with or not is up to you.

Answered By: OneCricketeer

For anyone who finds this question and wonders how to close the connection in this example, the following method worked for me: engine.dispose()

Answered By: twhyte
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.