title: Lost connection to MySQL server during query
body: <p>I have a huge table and I need to process all rows in it. I'm always getting this Lost connection message and I'm not able to reconnect and restore the cursor to the last position it was. This is basically the code I have here:</p>
  
  <pre><code>#
  import MySQLdb
  
  class DB:
    conn = None
  
    def connect(self):
      self.conn = MySQLdb.connect('hostname', 'user', '*****', 'some_table', cursorclass=MySQLdb.cursors.SSCursor)
  
    def query(self, sql):
      try:
       cursor = self.conn.cursor()
       cursor.execute(sql)
     except (AttributeError, MySQLdb.OperationalError):
       self.connect()
       cursor = self.conn.cursor()
       cursor.execute(sql)
     return cursor
  #
  
  #
  db = DB()
  sql = "SELECT bla FROM foo"
  data = db.query(sql)
  
  for row in data:
      do_something(row)
  #
  </code></pre>
  
  <p>But I'm always getting this:</p>
  
  <pre><code>#
  Traceback (most recent call last):
    File "teste.py", line 124, in &lt;module&gt;
     run()
   File "teste.py", line 109, in run
     for row in data:
   File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 417, in next
     row = self.fetchone()
   File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 388, in fetchone
     r = self._fetch_row(1)
   File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 285, in _fetch_row
     return self._result.fetch_row(size, self._fetch_type)
     _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
      Exception _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') in &lt;bound method SSCursor.__del__ of &lt;MySQLdb.cursors.SSCursor object at 0x7f7e3c8da410&gt;&gt; ignored
  #
  </code></pre>
  
  <p>Do you have any idea?</p>