如何使用 MySQLdb is_connected() 检查使用 python 的活动 mysql 连接?
How to use MySQLdb is_connected() to check an active mysql connection using python?
我想检查 mysql 连接是否有效。如何使用 MYSQLdb 在 Python 中做到这一点。我尝试了 docs 中的以下内容:
conn.is_connected()
这个 returns AttributeError: 'Connection' 对象没有属性 'is_connected'。
然后我尝试了:
conn.ping(attempts=1, delay=0)
这个 returns: TypeError: ping() 没有关键字参数
如何操作?
我认为你应该试试
if conn.open:
do some code...
您指向的文档是关于 mysql-connector
,而不是 MySQLdb
(又名 mysql-python
;好吧,有很多 Python 数据库 API MySQL 的驱动程序)。 MySQLdb 的文档位于 http://mysql-python.sourceforge.net/MySQLdb.html
Connection objects in MySQLdb 有带一个参数的 .ping()
方法,没有 .is_connected()
.
您正在寻找 conn.ping(True)
。
其实在MySQLdb中,更优雅的方式是不检查连接是否活跃。检查连接是否活跃如果mysql服务器悄悄关闭连接也会导致异常。留下它并在 try except 块中使用重新连接机制。
在MySQLdb中,connection.py中有一个errorhandler,当发生错误时会用到。
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
"""
If cursor is not None, (errorclass, errorvalue) is appended to
cursor.messages; otherwise it is appended to
connection.messages. Then errorclass is raised with errorvalue as
the value.
You can override this with your own error handler by assigning it
to the instance.
"""
error = errorclass, errorvalue
if cursor:
cursor.messages.append(error)
else:
connection.messages.append(error)
del cursor
del connection
raise errorclass, errorvalue
请参阅“del connection
”。它将关闭连接并将连接设置为 None。所以让错误发生,然后我们重建世界。
所以你只需要在 try except 块中实现一个重新连接机制来处理一些问题,比如 Mysql 已经消失了。
我不必使用 is_connection()
我的理解是您正在通过一些代码创建连接,例如
conn = MySQLdb.connect(host=host, user=username, passwd=password, db=database)
如果您打印 dir(conn) 的结果,它将向您显示该连接对象的所有属性和方法。当我打印它时,is_connection 不存在。有一个 属性 open
当您的连接打开时 returns 1
和当它关闭时 0
。我在我的代码中使用它来检查连接是否打开。
交互式 python 控制台中的小测试可以提供帮助:
if conn.open:
print('open')
else:
print('closed')
此外,如果您阅读文档:
open = <member 'open' of '_mysql.connection' objects>
True if connection is open
http://www.mikusa.com/python-mysql-docs/docs/MySQLdb.connections.html
我想检查 mysql 连接是否有效。如何使用 MYSQLdb 在 Python 中做到这一点。我尝试了 docs 中的以下内容:
conn.is_connected()
这个 returns AttributeError: 'Connection' 对象没有属性 'is_connected'。
然后我尝试了:
conn.ping(attempts=1, delay=0)
这个 returns: TypeError: ping() 没有关键字参数
如何操作?
我认为你应该试试
if conn.open:
do some code...
您指向的文档是关于 mysql-connector
,而不是 MySQLdb
(又名 mysql-python
;好吧,有很多 Python 数据库 API MySQL 的驱动程序)。 MySQLdb 的文档位于 http://mysql-python.sourceforge.net/MySQLdb.html
Connection objects in MySQLdb 有带一个参数的 .ping()
方法,没有 .is_connected()
.
您正在寻找 conn.ping(True)
。
其实在MySQLdb中,更优雅的方式是不检查连接是否活跃。检查连接是否活跃如果mysql服务器悄悄关闭连接也会导致异常。留下它并在 try except 块中使用重新连接机制。
在MySQLdb中,connection.py中有一个errorhandler,当发生错误时会用到。
def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
"""
If cursor is not None, (errorclass, errorvalue) is appended to
cursor.messages; otherwise it is appended to
connection.messages. Then errorclass is raised with errorvalue as
the value.
You can override this with your own error handler by assigning it
to the instance.
"""
error = errorclass, errorvalue
if cursor:
cursor.messages.append(error)
else:
connection.messages.append(error)
del cursor
del connection
raise errorclass, errorvalue
请参阅“del connection
”。它将关闭连接并将连接设置为 None。所以让错误发生,然后我们重建世界。
所以你只需要在 try except 块中实现一个重新连接机制来处理一些问题,比如 Mysql 已经消失了。
我不必使用 is_connection()
我的理解是您正在通过一些代码创建连接,例如
conn = MySQLdb.connect(host=host, user=username, passwd=password, db=database)
如果您打印 dir(conn) 的结果,它将向您显示该连接对象的所有属性和方法。当我打印它时,is_connection 不存在。有一个 属性 open
当您的连接打开时 returns 1
和当它关闭时 0
。我在我的代码中使用它来检查连接是否打开。
交互式 python 控制台中的小测试可以提供帮助:
if conn.open:
print('open')
else:
print('closed')
此外,如果您阅读文档:
open = <member 'open' of '_mysql.connection' objects>
True if connection is open
http://www.mikusa.com/python-mysql-docs/docs/MySQLdb.connections.html