AttributeError: type object ‘DatabaseManager’ has no attribute ‘cursor’

  mqtt, mysql, mysql-python, paho, python

I am importing a python function for storing mqtt data into database.
But while it executes, it generates Attribute error

This is the class

 class DatabaseManager():
        def __init__(self):
                while not hasattr(self, 'connection'):
                        config = {
                                 'user': 'root',
                                 'password': '[email protected]',
                                 'host': '192.168.99.100' if script_runs_within_container() else 'localhost',
                                 'port': '3306',
                                 'database': 'team'
                                 }
                        try:
                                self.connection = mysql.connector.connect(**config)
                                self.connection.execute('pragma foreign keys = on')
                                self.connection.commit()
                                self.cursor = self.connection.cursor()
                        except InterfaceError:
                                print("MySQL Container has not started yet. Sleep and retry...")
                                time.sleep(1)
        def add_del_update_db_record(self, sql_query, args=()):
                self.cursor.execute(sql_query, args)
                self.connection.commit()
                return
        def __del__(self):
                self.cursor.close()
                self.connection.close()

Python file containing this class also has function

def sensor_Data_Handler(Topic, jsonData):
        if Topic == "Home/BedRoom/DHT22/Temperature":
                DHT22_Temp_Data_Handler(jsonData)
        elif Topic == "Home/BedRoom/DHT22/Humidity":
                DHT22_Humidity_Data_Handler(jsonData)   

Which I imported to other file.

While executing, it shows the following error

 Exception ignored in: <function DatabaseManager.__del__ at 0x7f5c1c7e3c80>
mqtt_listening_1  | Traceback (most recent call last):
mqtt_listening_1  |   File "/store_data/store_Sensor_Data_to_DB.py", line 32, in __del__
mqtt_listening_1  |     self.cursor.close()
mqtt_listening_1  | AttributeError: 'DatabaseManager' object has no attribute 'cursor'
mqtt_listening_1  | Exception ignored in: <function DatabaseManager.__del__ at 0x7f5c1c7e3c80>
mqtt_listening_1  | Traceback (most recent call last):
mqtt_listening_1  |   File "/store_data/store_Sensor_Data_to_DB.py", line 32, in __del__
mqtt_listening_1  |     self.cursor.close()

I am actually trying to continuously store the data thus each time function is called, it is showing this error

The other two function of sensor_data_handler is invoking are

def DHT22_Temp_Data_Handler(jsonData):
        json_Dict = json.loads(jsonData)
        SensorID = json_Dict['Sensor_ID']
        Temperature = json_Dict['Temperature']
        #Push into DB Table
        dbObj = DatabaseManager()
        insert_stmt = (
                "INSERT INTO DHT22_Temperature_Data (SensorID, Temperature) "
                "VALUES (%s,%s)")
        dbObj.add_del_update_db_record( insert_stmt ,(SensorID,Temperature))
        del dbObj
        print("Inserted Temperature Data into Database.")
        print("")

Source: Python Questions

LEAVE A COMMENT