pandas 熊猫需要关闭连接吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42034373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:54:18  来源:igfitidea点击:

Does pandas need to close connection?

pythonpandas

提问by deez

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?

使用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()

采纳答案by OneCricketeer

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

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

I'd close for safe-measure. Whether you do that using withor not is up to you.

我会关闭以采取安全措施。使用with与否取决于您。

回答by twhyte

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

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