Skip to content

Instantly share code, notes, and snippets.

@jeremyyeo
Last active July 12, 2024 06:21
Show Gist options
  • Save jeremyyeo/d93b93d15f8788b1799f8e3ebef1d6d9 to your computer and use it in GitHub Desktop.
Save jeremyyeo/d93b93d15f8788b1799f8e3ebef1d6d9 to your computer and use it in GitHub Desktop.
Test connecting to MS Fabric
# pip install pyodbc
import pyodbc
# From Entra.
# https://prodata.ie/2023/11/15/service-principal-fabric/
service_principal_client_id = "<Client ID>"
service_principal_client_secret = "<Client Secret>"
# From Fabric UI - there is a 'SQL connection string' that we will use as the 'server'.
server = "<guid>-<guid>.datawarehouse.fabric.microsoft.com"
# From Fabric UI - the name of the warehouse is used as the 'database'.
database = "<Fabric Warehouse Name>"
# Microsoft ODBC drivers have to be installed:
# https://learn.microsoft.com/nl-be/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos
driver = "ODBC Driver 18 for SQL Server"
conn_str =(
f"DRIVER={driver};"
f"SERVER={server};"
f"DATABASE={database};"
f"UID={service_principal_client_id};"
f"PWD={service_principal_client_secret};"
f"Authentication=ActiveDirectoryServicePrincipal"
)
conn = pyodbc.connect(conn_str)
curr = conn.cursor()
curr.execute("select cast(current_timestamp as varchar) as ts")
for row in curr.fetchall():
print(row)
# ('Jul 12 2024 2:54AM',)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment