Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Microsoft Azure connection strings take the following form:

mssql+pyodbc:///?odbc_connect=<quoted

...

connection

...

string>

The <quoted connection string> value is difficult to generate by hand as it requires escaping many characters. To generate a connection string for Azure databases, it is recommended to use the following Python code:

Generating a connection string
Code Block
languagepytitleGenerating a connection string
from urllib.parse import quote_plus

# change these values to match your Azure database
server = 'tcp:your-azure-server.database.windows.net'
database = 'your-database-nametest'
username = 'yourusername'
password = '***'   

# these should not usually need to change
driver = '{ODBC Driver 18 for SQL Server}'
port = 1433

odbc_str = f'DRIVER={driver};SERVER={server};PORT={port};DATABASE={database};UID={username};PWD={password}'
sqlalchemy_str = 'mssql+pyodbc:///?odbc_connect=' + quote_plus(odbc_str)

print(sqlalchemy_str)  # use this value in the data export tool

# optionally test your connection
from sqlalchemy import create_engine
engine = create_engine(sqlalchemy_str)
query = "select * from <table_name>"
print(engine.execute(query).fetchall())