How to connect to a server cluster using python?
I am trying to setup a connection string to a sever cluster instance in python after a few searches here and there I seem to be stumped, regardless of what solution I try I get one of two errors. Either the cluster dose not exist or back slash is not allowed in connection string. I tried using raw strings to by bass the backslash error but the error returned to server cannot be found. I have double checked/triple checked the credentials. I am able to connect from other applications and services in java but I am having issues with my python scripts. Any suggestions would help thank you for your time in advance.
DRIVER_NAME = 'ODBC Driver 17 for SQL Server'
SERVER_NAME = 'SERVER_NAME'
INSTANCE = 'INSTANCE'
USERNAME = 'USERNAME'
PASSWORD = 'PASSWORD'
CONNECTION_STRING = f"""DRIVER={{{DRIVER_NAME}}};SERVER={SERVER_NAME};DATABASE={DATABASE_NAME};INSTANCE{INSTANCE};UID={USERNAME};PWD={PASSWORD}"""
DRIVER_NAME = 'SQL Server'
SERVER_NAME = r'SERVER_NAME/INSTANCE'
USERNAME = 'USERNAME'
PASSWORD = 'PASSWORD'
CONNECTION_STRING = f"""DRIVER={{{DRIVER_NAME}}};SERVER={SERVER_NAME};DATABASE={DATABASE_NAME};UID={USERNAME};PWD={PASSWORD}"""
try:
cnxn = pyodbc.connect(CONNECTION_STRING)
cursor = cnxn.cursor()
the code fails at pyodbc.connect(CONNECTION_STRING)
Error in insertIntoDB: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')