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
Question Two arguments in input function Alfredd 4 737 Nov-09-2025, 12:56 AM
Last Post: Pedroski55
  function arguments Curbie 2 1,010 Apr-30-2025, 05:23 PM
Last Post: Curbie
  pass arguments from bat file to pyhon script from application absolut 2 2,764 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  How to pass encrypted pass to pyodbc script tester_V 0 2,330 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 6 3,884 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 7,785 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 4,416 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 3,604 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 3,581 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 10,387 Jan-18-2021, 12:08 PM
Last Post: Serafim

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.