Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vinicius-solon-silva/ee533e025f4d9cc4e8e3a0e72e111082 to your computer and use it in GitHub Desktop.

Select an option

Save vinicius-solon-silva/ee533e025f4d9cc4e8e3a0e72e111082 to your computer and use it in GitHub Desktop.
SQLAlchemy connect to Azure SQL, using PyODBC
# On mac, run these first:
# - brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
# - brew update
# - brew install msodbcsql17 mssql-tools
#
# Install requirements:
# - pip install pyodbc sqlalchemny
import urllib
from sqlalchemy import create_engine
server = "<YOUR_AZURE_DB_NAME>.database.windows.net"
database = "<DB NAME>"
username = "<USER NAME>"
password = "<PASSWORD>"
driver = '{ODBC Driver 17 for SQL Server}'
odbc_str = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;UID='+username+';DATABASE='+ database + ';PWD='+ password
connect_str = 'mssql+pyodbc:///?odbc_connect=' + urllib.quote_plus(odbc_str)
print connect_str
engine = create_engine(connect_str)
print engine.execute("SELECT 1").fetchall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment