I am trying to access an Azure SQL database from Python code, and try as I might, I can't make it work

Steve Gray 40 Reputation points
2025-11-19T22:37:34.0466667+00:00
Here's the code that's doing the connection.  I also really don't understand where the users and their passwords are set. The error I get is this, and the code is below it.  Thank you so much for any advice.  (I've blanked out the UID and password I've been trying to use.)

DB connection error: ('HY000', '[HY000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot open server "iwotgames.com" requested by the login.  The login failed. (40532) (SQLDriverConnect); [HY000] [Microsoft][ODBC Driver 18 for SQL Server]Invalid connection string attribute (0); [HY000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot open server "iwotgames.com" requested by the login.  The login failed. (40532); [HY000] [Microsoft][ODBC Driver 18 for SQL Server]Invalid connection string attribute (0)')

-------------

conn_str = (
    "Driver={ODBC Driver 18 for SQL Server};"
    "Server=tcp:iwot-generative.database.windows.net,1433;"
    "Database=iwot-generation-record;"
    "Uid=XXX;"
    "Pwd=YYY;"
    "Encrypt=yes;"
    "TrustServerCertificate=yes;"
    "Connection Timeout=60;"
)
print(conn_str)

self.db_connection = pyodbc.connect(conn_str) 
SQL Server Database Engine
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 69,140 Reputation points MVP Volunteer Moderator
    2025-11-19T23:45:25.9633333+00:00

    Follow https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-python-quickstart?view=azuresql&tabs=windows%2Csql-inter

    export AZURE_SQL_CONNECTIONSTRING='Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'

    or

    export AZURE_SQL_CONNECTIONSTRING='Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;UID=<user-name>;PWD=<user-password>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


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.