Python丢失了Mysql Server连接

时间:2011-06-22 22:12:07

标签: android python mysql

我正在将python连接到mySqL数据库并从android发送两个Double值以存储在Mysql中然后我收到此错误

enter code here
 Exception in thread :
 Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 530, in __bootstrap_inner
self.run()
  File "C:\Users\ingy\Desktop\New folder (2)\sssssss.py", line 64, in run
self.pacman()
 File "C:\Users\ingy\Desktop\New folder (2)\sssssss.py", line 45, in pacman
x.execute("UPDATE location SET x='%s'"%(x1))
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
 File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in  defaulterrorhandler
 raise errorclass, errorvalue
 OperationalError: (2006, 'MySQL server has gone away')

现在

   enter code here
         OperationalError: (2013, 'Lost connection to MySQL server during query')

2 个答案:

答案 0 :(得分:2)

这就是我所做的,在我们的情况下,它只会在最坏的情况下消失一次并抛出不同的错误,所以我们不会得到无限的重复。

def query(self, sql, parameters=None):
    cursor = self.db.cursor()
    try:
        cursor.execute(sql, parameters)
        return cursor
    except mysql.connector.Error as ex:
        if ex.errno == 2006: # mysql has gone away
            cursor.close()
            self.connect()
            return self.query(sql, parameters)
        raise ex

答案 1 :(得分:1)

这通常发生在您的应用程序闲置时间过长且无法与MySQL通信时。你可以:

  • 增加mysql服务器连接超时(在您的情况下为wait_timeout
  • 不时发送虚拟MySQL查询SELECT 1

如果您可以访问MySQL的连接设置,请务必使用第一个。