Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fix "login failed for user.." error in connecting Microsoft SQL server in Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading