Python to SQL connection issues

qp 26 Reputation points
2021-04-21T15:14:41.777+00:00

Try to connect Python to SQL with this
import pyodbc
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=DELLPRECISION;"
"Database=AventureWork2017;"
"UID=DELLPRECISION\sa"
"PWD=password;"
"Trusted_Connection=no;")
and got error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DELLPRECISION\sa'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DELLPRECISION\sa'. (18456)")

Not sure why?

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

CathyJi-MSFT 22,431 Reputation points Microsoft External Staff
2021-04-22T02:44:50.18+00:00

Hi @qp ,

> Login failed for user 'DELLPRECISION\sa'. (18456)"

Please make sure the login 'DELLPRECISION\sa' is existed and the login is enabled for this SQL server instance. Check this from SSMS > under the Object Explorer menu > Security > Logins. Please also make sure the login is spelled correct and the password is correct.

If you adding the user ID and PWD, suggest you removing the ‘Trusted_Connection’ parameter in connection string.

Please refer to the blog How to Connect Python to SQL Server using pyodbc and the thread Connecting to Microsoft SQL server using Python to get more detail information.


If the response is helpful, please click "Accept Answer" and upvote it, thank you.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. qp 26 Reputation points
    2021-04-21T17:21:19.59+00:00

    I did and same error.

    Was this answer helpful?

    0 comments No comments

  2. Guoxiong 8,221 Reputation points
    2021-04-21T16:29:54.703+00:00

    Did you try UID=sa instead of UID=DELLPRECISION\sa?

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.