Home > Back-end >  Fix "login failed for user.." error in connecting Microsoft SQL server in Python
Fix "login failed for user.." error in connecting Microsoft SQL server in Python

Time:05-20

I am receiving a very common error while trying to connect Microsoft SQL Server using pyodbc. I've tried the solutions suggested here before, but none of them solved my problem.

import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server= server_name;'
                      'Database= database_name;'     
                      'UID= user_id;'
                      'PWD= password;')


InterfaceError: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]
Login failed for user ' '. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]
[SQL Server]Login failed for user ' '. (18456)")

When I connect the database via Microsoft SQL Server Management Studio 18 app, below is how I connect. I copy and paste server name, login, and password information to my Python code as shown above. My OS is Windows 10. When I call select @@version, I get Microsoft SQL Server 2014 (SP3-GDR) (KB4583463)...

enter image description here

CodePudding user response:

Admittedly my python skills are not the best but I'm suspicious of all the single quotes in your connection string.

In the error message Login failed for user ' ' is that blank value for the use what you actually see or have you stripped that out? If that's what you actually see then I think your connection string is malformed.

Refer to the sample here for how to build out a connection string.

  • Related