- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 04:22 AM - edited 09-19-2023 04:37 AM
First, you need to create a service principal in Azure and grant it the necessary permissions to access your Azure SQL Database to do crm data enrichment. You can do this using the Azure CLI or the Azure Portal. Ensure that your Databricks cluster has the necessary libraries installed. You will need to use the pyodbc library to connect to Azure SQL Database. You can install it using the following command in a Databricks notebook cell:
%pip install pyodbc
Python Code:
Here's a Python code snippet to connect to Azure SQL Database using a Service Principal:
import pyodbc
# Define the connection string
server = "your_server_name.database.windows.net"
database = "your_database_name"
clientId = "your_service_principal_client_id"
clientSecret = "your_service_principal_client_secret"
authority = "https://login.microsoftonline.com/your_tenant_id"
connection_string = (
f"Driver={{ODBC Driver 17 for SQL Server}};"
f"Server={server};"
f"Database={database};"
f"Authentication=ActiveDirectoryServicePrincipal;"
f"UID={clientId};"
f"PWD={clientSecret};"
f"Encrypt=yes;"
f"TrustServerCertificate=no;"
)
try:
# Establish the database connection
conn = pyodbc.connect(connection_string)
cursor = conn.cursor()
# Execute SQL queries here
cursor.execute("SELECT * FROM your_table_name")
rows = cursor.fetchall()
for row in rows:
print(row)
except Exception as e:
print(f"Error: {str(e)}")
finally:
# Close the connection
conn.close()
Replace your_server_name, your_database_name, your_service_principal_client_id, your_service_principal_client_secret, your_tenant_id, and your_table_name with your actual Azure SQL Database and Service Principal information.