0
votes

I have searched the web all day tried many solutions but none works. I can manually connect to this server but not with Python I get this error:

pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (5); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

I have tried the following:

  • A)

    cnx = pyodbc.connect(Driver='{SQL Server}',
                         SERVER=self.DBserver,
                         DATABASE=xyz,
                         username=self.DBusername,
                         password=self.DBpassword,
                         PORT = 1433)
    
  • B)

    cnx = pyodbc.connect('''Driver={SQL Server}; 
                         SERVER=tcp:<self.DBserver>; 
                         PORT=self.DBPort; DATABASE=xyz; 
                         UID=self.DBusername; 
                         PWD=self.DBpassword''')
    
  • C)

    cnx = = pyodbc.connect(Driver='{SQL Server}',
                           SERVER=self.DBserver,
                           DATABASE=xyz,
                           UID=self.DBusername,
                           PWD=self.DBpassword)
    
2
Your second example won't work as written because you will be passing the literal self.DBusername not the value of that property. However, your second example does use the correct attribute names UID= and PWD=. Also, Microsoft's ODBC drivers do not use PORT=, but you've shown the port as 1433 so it presumably doesn't matter because that's the default. Try your second approach using an f'''string''' and see if that helps.Gord Thompson
so i added A, B, C tries the try C above is your suggestion that didnt help . Unless I didnt understand your approachGhost
Try hard-coding parameter values. It might the self variables do not propagate.Parfait

2 Answers

0
votes

I'm guessing you need something more like this:

cnx = pyodbc.connect('DRIVER={SQL Server};SERVER='+self.DBserver+';DATABASE=xyz;UID='+self.DBusername+';PWD='+self.DBpassword)
0
votes

The issue was with firewall. it is working now