EDIT:
I added self in __init__(self). And I removed query from def select(self, query) because you define it inside function so sending it as parameter is useless.
Inside __init__ you create local variable driver_path and it doesn't exist in other functions.
You have to use self.driver_path to access it in other functions in class.
class Inputdata:
#def __init__(): # OLD VERSION
def __init__(self):
self.driver_path = ("Driver={SQL Server};"
"Server=xxx;"
"Database=xxx;"
"Trusted_Connection=xxx;")
#def select(self, query): # OLD VERSION
def select(self): # you don't need `con`, and you don't need `query` because you define it inside function
con = pyodbc.connect(self.driver_path)
query = "SELECT * FROM TABLE "
result = pd.read_sql(query, con)
return result
# --- main ---
x = Inputdata()
data = x.select()
EDIT:
Eventually you could create con in __init__ and it would need self. to access it in other functions
class Inputdata:
#def __init__(): # OLD VERSION
def __init__(self):
driver_path = ("Driver={SQL Server};"
"Server=xxx;"
"Database=xxx;"
"Trusted_Connection=xxx;")
self.con = pyodbc.connect(driver_path)
#def select(self, query): # OLD VERSION
def select(self): # you don't need `con`, and you don't need `query` because you define it inside function
query = "SELECT * FROM TABLE "
result = pd.read_sql(query, self.con)
return result
# --- main ---
x = Inputdata()
data = x.select()