Search code examples
pythonazureconnection

Logon failed (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxx'


I have big issues to connect to the sql sever with python 3.8. I wonder if I need to downgrade to 3.7 python and if there is something else I am doing wrong?

HERE IS MY CODE AND ERROR MESSAGE:

params = urllib.parse.quote_plus \
('Driver={SQL 
Server};Server=tcp:xxxxx.database.windows.net,1433; 
Database=GroceryDB;Uid=xxxxx;Pwd= 
{xxxx};Encrypt=no;Connection Timeout=30;')

conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine = 
sqlalchemy.engine.create_engine(conn_str,echo=False,pool_pre_ 
ping=True)

Logon failed (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxx'.


Solution

  • NEWEST

    You can follow the document(Open the ODBC Data Source Administrator) to check the odbc driver whether installed successfully.

    enter image description here

    PRIVIOUS

    Below code works for me.

    from urllib import parse
    import pyodbc
    from sqlalchemy import create_engine
    import urllib
    
    params = parse.quote_plus \
    (r'Driver={ODBC Driver 17 for SQL Server};Server=tcp:yoursqlserver.database.windows.net,1433;Database=dbname;Uid=sasasa;Pwd={pwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
    
    conn_str="mssql+pyodbc:///?odbc_connect={}".format(params)
    
    engine= create_engine (conn_str,echo=True)
    
    connection = engine.connect()
    result = connection.execute("select 1+1 as res")
    for row in result:
    print("res:", row['res'])
    connection.close()
    

    enter image description here