Python lose Mysql Server connection
i'm connecting python to mySqL Databases and sending things from android two Double values to be store in Mysql then i get this error
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')
Now
enter 开发者_JAVA百科code here
OperationalError: (2013, 'Lost connection to MySQL server during query')
This is what I do, in our case it only goes away once in the worst case and throws a different error, so we don't get infinite recurision.
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
This usually takes place when your application is idling for too long and not communicating with MySQL. You can either:
- increase the mysql servers connection timeout (in your case the
wait_timeout
) - send out dummy MySQL queries from time to time
SELECT 1
If you have access to MySQL's connections settings definitely go with the first one.
精彩评论