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
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You're right. It's actually working.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 803 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,485 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,113 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,127 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 5,525 Jan-18-2021, 12:08 PM
Last Post: Serafim
  how to pass arguments between pythons scripts? electricDesire 2 2,110 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Do I have to pass 85 variables to function? Milfredo 10 4,188 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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