大熊猫需要关闭连接吗?

dee*_*eez 21 python pandas

使用pandas"read_sql_query"时,是否需要关闭连接?或者我应该使用"with"语句?或者我可以使用以下内容并保持良好状态?

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()
Run Code Online (Sandbox Code Playgroud)

小智 14

对于发现此问题且想知道如何在此示例中关闭连接的任何人,以下方法对我有用:engine.dispose()


cri*_*007 9

查看源代码,我找不到con.close()任何SQL连接对象的方法,只cursor查找查询的对象.

我会关闭安全措施.无论您是否使用with,都取决于您.

  • 连接超出范围时通常应关闭。也就是说,最好在适当的时候关闭连接。 (2认同)