Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass Arguments to Function
#1
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
Reply


Messages In This Thread
Pass Arguments to Function - by phillyfa - Mar-26-2020, 07:21 PM
RE: Pass Arguments to Function - by buran - Mar-26-2020, 07:28 PM
RE: Pass Arguments to Function - by phillyfa - Mar-27-2020, 12:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass arguments from bat file to pyhon script from application absolut 2 916 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  How to pass encrypted pass to pyodbc script tester_V 0 1,707 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 6 2,840 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 5,923 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 3,529 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,905 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,877 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 8,262 Jan-18-2021, 12:08 PM
Last Post: Serafim
  how to pass arguments between pythons scripts? electricDesire 2 2,981 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Do I have to pass 85 variables to function? Milfredo 10 6,129 Sep-26-2020, 10:13 PM
Last Post: Milfredo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020