Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pass Arguments to Function

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):
        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:

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!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 63 5 hours ago
Last Post: bowlofred
  Function with many arguments, with some default values medatib531 3 169 Mar-14-2020, 02:39 AM
Last Post: medatib531
  How to pass variable as an argument to be used in a function? t4keheart 8 400 Jan-16-2020, 01:37 PM
Last Post: buran
  Pass by reference vs Pass by value leodavinci1990 1 210 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Using function *args to multiply multiple arguments allusernametaken 8 365 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  How to access arguments by name from function SriRajesh 7 286 Sep-19-2019, 07:14 PM
Last Post: ichabod801
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 644 May-11-2019, 03:18 AM
Last Post: keames
  readshapefile function in basemap - confusion regarding arguments sendiptangshu 1 469 Mar-09-2019, 09:29 AM
Last Post: Larz60+
  __init__ function arguments CoderMan 4 566 Jan-27-2019, 02:04 PM
Last Post: stullis
  Pass 2x Variables from Function to another Function MTom5 3 641 Aug-14-2018, 02:39 PM
Last Post: MTom5

Forum Jump:

Users browsing this thread: 1 Guest(s)