SQLAlchemy连接挂起

2024-06-25 22:39:51 发布

您现在位置:Python中文网/ 问答频道 /正文

def get_engine():
    engine = create_engine('mysql+mysqlconnector://...my_conn_string...', echo=True)
    return engine

def generic_execute(sql):
    db = get_engine()
    connection = db.connect()
    connection.execute(sql)

上面的代码正确地执行了查询,但似乎无限挂起。在

如何正确地“关闭”或“终止”这个连接?非常感谢你!在


Tags: echotrueexecutedbsqlgetstringmy
1条回答
网友
1楼 · 发布于 2024-06-25 22:39:51

正如您所说的,需要按照documentation关闭连接。 因此,在执行完sql查询后,您需要调用:

connection.close()

另外,如果您已经完成了引擎db,您可以调用db.dispose()来清理所有内容。在

相关问题 更多 >