Sep-30-2022, 07:10 PM
Hi Team,
how to create a sql connection function , and validate if it is successfully connected it or not.
I attempted with below code. code works but not sure whether this is a correct one.
how to create a sql connection function , and validate if it is successfully connected it or not.
I attempted with below code. code works but not sure whether this is a correct one.
def Create_Connection(server,dbname): try: str = ('Driver={SQL Server};' f'Server={server};' f'Database={dbname} ;') conn = pyodbc.connect(str) print('connected Successfully') return conn except: print('error occured') if __name__ == "__main__": server = 'DESKTOP-GQK64O6' dbname = 'Customer' con = Create_Connection(server, dbname) query = "select * from employee"