
class Database:
    def __init__(self, db):
        self.db = db
        self.cursor = db.cursor()

    def disconnect(self):
        self.cursor.close()
        self.db.close()

    def query(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def insert(self, sql):
        self.cursor.execute(sql)
        self.db.commit()



import pymysql

class Database:
    def __init__(self, db):
        self.db = db
        self.cursor = db.cursor()

    def disconnect(self):
        self.cursor.close()
        self.db.close()

    def query(self, sql):
        try:
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except pymysql.OperationalError:
            self.db.ping()
            self.cursor.execute(sql)
            return self.cursor.fetchall()

    def insert(self, sql):
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except pymysql.OperationalError:
            self.db.ping()
            self.cursor.execute(sql)
            self.db.commit()

Reconnect if the connection times out.
Reconnect if the connection times out.

+ import pymysql
  
  class Database:
      def __init__(self, db):
          self.db = db
          self.cursor = db.cursor()
  
      def disconnect(self):
          self.cursor.close()
          self.db.close()
  
      def query(self, sql):
+         try:
-         self.cursor.execute(sql)
+             self.cursor.execute(sql)
-         return self.cursor.fetchall()
+             return self.cursor.fetchall()
+         except pymysql.OperationalError:
+             self.db.ping()
+             self.cursor.execute(sql)
+             return self.cursor.fetchall()
  
      def insert(self, sql):
+         try:
-         self.cursor.execute(sql)
+             self.cursor.execute(sql)
-         self.db.commit()
+             self.db.commit()
+         except pymysql.OperationalError:
+             self.db.ping()
+             self.cursor.execute(sql)
+             self.db.commit()
  
- 
- 
Reconnect if the connection times out.
## Code Before:

class Database:
    def __init__(self, db):
        self.db = db
        self.cursor = db.cursor()

    def disconnect(self):
        self.cursor.close()
        self.db.close()

    def query(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def insert(self, sql):
        self.cursor.execute(sql)
        self.db.commit()



## Instruction:
Reconnect if the connection times out.
## Code After:
import pymysql

class Database:
    def __init__(self, db):
        self.db = db
        self.cursor = db.cursor()

    def disconnect(self):
        self.cursor.close()
        self.db.close()

    def query(self, sql):
        try:
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except pymysql.OperationalError:
            self.db.ping()
            self.cursor.execute(sql)
            return self.cursor.fetchall()

    def insert(self, sql):
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except pymysql.OperationalError:
            self.db.ping()
            self.cursor.execute(sql)
            self.db.commit()
