Mar-26-2020, 07:21 PM
Hello,
The following function creates a connection string to a SQL Server instance. I have another program where all the values are hard-coded into the connection string. However, I'm toying around with different scenarios and would like to create a generic connetion string where arguments for host, database, username, etc could be passed to the function when it's called.
Here's what I have so far:
The following function creates a connection string to a SQL Server instance. I have another program where all the values are hard-coded into the connection string. However, I'm toying around with different scenarios and would like to create a generic connetion string where arguments for host, database, username, etc could be passed to the function when it's called.
Here's what I have so far:
import pyodbc
'''
Connect to the database by the given credentials
'''
def connect_to_database(driver, host, database):
try:
conn = pyodbc.connect('DRIVER='+driver+'; SERVER='+host+'; DATABASE='+database+';trusted_connection=yes;')
return conn
except Exception as e:
print ("Error while connecting to database", e)
connect_to_database("SQL Server", "MJ0", "AdventureWorks2012")But I'm receiving the following error:Error: server (str) = "MJ0"
^
SyntaxError: cannot assign to function call
