Python Forum

Full Version: Pass Arguments to Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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
obviously this is not the code that produce the error - there is no server (str) = "MJ0" in this code.
Check and post your actual code, as well as the full traceback, not just the last line
You're right. It's actually working.

Thx for the second set of eyes and sorry for the confusion!