You can download this code by clicking the button below.
This code is now available for download.
This function uses the pyodbc library to connect to a specified database. The user needs to provide the driver name, server, database name, username, and password.
Technology Stack : pyodbc
Code Type : Function
Code Difficulty : Intermediate
import pyodbc
def connect_to_database(driver, server, database, username, password):
"""
Connects to a database using pyodbc.
"""
try:
connection = pyodbc.connect('DRIVER={'+driver+'};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
print("Connection successful")
return connection
except Exception as e:
print("Error connecting to the database: ", e)
return None