The question says everything. How can I check if my MySQL connection is closed in Python?
I'm using MySQLdb, see http://mysql-python.sourceforge.net/
The question says everything. How can I check if my MySQL connection is closed in Python?
I'm using MySQLdb, see http://mysql-python.sourceforge.net/
The Connection.open field will be 1 if the connection is open and 0 otherwise. So you can say
if conn.open:
# do something
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.