gpt4 book ai didi

python - pyMySQL:如何检查连接是否已经打开或关闭

转载 作者:可可西里 更新时间:2023-11-01 07:48:01 32 4
gpt4 key购买 nike

我收到错误 InterfaceError (0, '')Pymysql 库中有没有办法我可以检查连接或游标是否关闭。对于游标,我已经在使用这样的上下文管理器:

with db_connection.cursor() as cursor:
....

最佳答案

您可以使用 Connection.open 属性。

如果连接打开,Connection.open 字段将为 1,否则为 0。所以你可以说

if conn.open:
# do something

The conn.open attribute will tell you whether the connection has been explicitly closed or whether a remote close has been detected. However, it's always possible that you will try to issue a query and suddenly the connection is found to have given out - there is no way to detect this ahead of time (indeed, it might happen during the process of issuing the query), so the only truly safe thing is to wrap your calls in a try/except block

关于python - pyMySQL:如何检查连接是否已经打开或关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795662/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com