'AttributeError: 'DataModelPostgressql' object has no attribute 'conn'

import psycopg2
import pandas as pd
class DataModelPostgressql():
    def __init__(self,host,port,databaseName,username,password):
        self.host=host
        self.port=port
        self.databaseName=databaseName
        self.username=username
        self.password=password

    def create_connection(self):
        conn = psycopg2.connect(
            database=self.databaseName, user=self.username, password=self.password, host=self.host, port=self.port
        )
        #cursor = self.conn.cursor()
        #return conn

    def queryResult(self,query):
        
        if self.conn:
            cur = self.conn.cursor()
            cur.execute(query)
            df = pd.DataFrame(cur.fetchall())

        return df

    def close_Connection(self):
        if self.conn:
            self.cursor.close()
            self.conn.close()
        

query="SELECT * FROM COMPANY"
Obj1= DataModelPostgressql('localhost',5432,'postgres','postgres','postgres')
Obj1.queryResult(query)


Having error like AttributeError: 'DataModelPostgressql' object has no attribute 'conn' I want to create a database connection in one method and read data from another method and close database connection with another method Please help me to solve this



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source