I'm writing a local python script that queries an MSSQL database that runs locally and having difficulties connecting to the database. The database runs inside a docker container since I'm using a mac (i5). I'm not sure what is the correct format for the connection string required to create the engine using sqlalchemy. I use Azure Data Studio to query the database directly. How can I check for the connection string from the docker desktop app or the Azure Data studio app?
I ran the following code:
def generate_engine(path_to_yml):
# Load credentials from the .yml file
credentials_file_path = path_to_yml
credentials = load_credentials(credentials_file_path)
if credentials:
server = credentials['server']
port = credentials['port']
database = credentials['database']
username = credentials['username']
password = credentials['password']
# Construct the connection string
connection_string = f'mssql+pyodbc://{username}:{password}@{server}:{port}/{database}?driver=ODBC+Driver+17+for+SQL+Server'
# Create the engine
engine = create_engine(connection_string) # Set echo=True for debugging
# Test the connection
try:
connection = engine.connect()
print("Connection successful!")
connection.close()
except Exception as e:
print("Connection error:", e)
and got the following error:
Connection error: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "mcr.microsoft.com/mssql/server:2019-latest" requested by the login. The login failed. (4060) (SQLDriverConnect)')
This is my yaml file:
server: localhost
port: 1433
database: mcr.microsoft.com/mssql/server:2019-latest
username: sa
password: #mypassword
I'm not sure i put the correct string in the database field in the yaml file and in the end of the connection field.
Thanks!