Skip to content

Commit a68036f

Browse files
Merge pull request #311308 from dlevy-msft-sql/mssql-python-migration
Update Python code samples to use mssql-python instead of pyodbc
2 parents ca25aa7 + 0a44b42 commit a68036f

7 files changed

Lines changed: 43 additions & 44 deletions

File tree

articles/app-service/includes/tutorial-connect-msi-azure-database/code-sql-mi.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,27 @@ For more information, see [Connect using Microsoft Entra authentication](/sql/co
7979
8080
1. Install dependencies.
8181
```bash
82-
python -m pip install pyodbc
82+
python -m pip install mssql-python python-dotenv
8383
```
8484
8585
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. Uncomment the part of the code snippet for the authentication type you want to use.
8686
```python
87-
import os;
88-
import pyodbc
87+
import os
88+
from mssql_python import connect
8989
9090
server = os.getenv('AZURE_SQL_SERVER')
9191
port = os.getenv('AZURE_SQL_PORT')
9292
database = os.getenv('AZURE_SQL_DATABASE')
93-
authentication = os.getenv('AZURE_SQL_AUTHENTICATION') # The value should be 'ActiveDirectoryMsi'
9493
9594
# Uncomment the following lines according to the authentication type.
9695
# For system-assigned managed identity.
97-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};Authentication={authentication};Encrypt=yes;'
96+
# connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;'
9897
9998
# For user-assigned managed identity.
10099
# client_id = os.getenv('AZURE_SQL_USER')
101-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={client_id};Authentication={authentication};Encrypt=yes;'
100+
# connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;'
102101
103-
conn = pyodbc.connect(connString)
102+
conn = connect(connection_string)
104103
```
105104
For an alternative method, you can also connect to Azure SQL Database using an access token, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python).
106105

articles/app-service/troubleshoot-intermittent-outbound-connection-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Although PHP doesn't support connection pooling, you can try using persistent da
103103
* [MySQL](https://dev.mysql.com/doc/connector-python/en/connector-python-connection-pooling.html)
104104
* [MariaDB](https://mariadb.com/docs/connectors/mariadb-connector-python/api/pool/)
105105
* [PostgreSQL](https://www.psycopg.org/docs/pool.html)
106-
* [Pyodbc](https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling)
106+
* [mssql-python](https://github.com/microsoft/mssql-python) (recommended for SQL Server)
107107
* [SQLAlchemy](https://docs.sqlalchemy.org/en/20/core/pooling.html)
108108

109109
HTTP connection pooling

articles/azure-functions/recover-python-functions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ If your function app is using the Python pickle library to load a Python object
235235

236236
### Pyodbc connection collision
237237

238-
If your function app is using the popular ODBC database driver [pyodbc](https://github.com/mkleehammer/pyodbc), it's possible that multiple connections are open within a single function app. To avoid this issue, use the singleton pattern, and ensure that only one pyodbc connection is used across the function app.
238+
If your function app is using the ODBC database driver [pyodbc](https://github.com/mkleehammer/pyodbc), it's possible that multiple connections are open within a single function app. To avoid this issue, use the singleton pattern, and ensure that only one pyodbc connection is used across the function app.
239+
240+
> [!TIP]
241+
> Consider using [mssql-python](https://github.com/microsoft/mssql-python), Microsoft's official Python driver for SQL Server, which provides built-in Microsoft Entra authentication support and doesn't require manual ODBC driver management.
239242
240243
---
241244

articles/service-connector/includes/code-fabricsql-me-id.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,24 @@ spring:
8888
8989
1. Install dependencies.
9090
```bash
91-
python -m pip install pyodbc
91+
python -m pip install mssql-python python-dotenv
9292
```
9393
94-
1. Retrieve the SQL database in Microsoft Fabric connection string from the environment variable added by Service Connector. If you are using Azure Container Apps as compute service or the connection string in the code snippet doesn't work, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python#update-the-local-connection-configuration) to connect to SQL database in Microsoft Fabric using passwordless credentials. `Authentication=ActiveDirectoryMSI;` is required in the connection string when connecting using managed identities. `UID=<msiClientId>` is also required in the connection string when connecting using a user-assigned managed identity.
94+
1. Retrieve the SQL database in Microsoft Fabric connection string from the environment variable added by Service Connector. `Authentication=ActiveDirectoryMSI;` is required in the connection string when connecting using managed identities. `UID=<msiClientId>` is also required in the connection string when connecting using a user-assigned managed identity.
9595
9696
```python
9797
import os
98-
import pyodbc, struct
99-
from azure.identity import DefaultAzureCredential
98+
from mssql_python import connect
10099
101-
connStr = os.getenv('FABRIC_SQL_CONNECTIONSTRING')
100+
connection_string = os.getenv('FABRIC_SQL_CONNECTIONSTRING')
102101
103102
# System-assigned managed identity connection string format
104-
# `Driver={ODBC Driver 17 for SQL Server};Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;Authentication=ActiveDirectoryMSI;`
103+
# `Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;Authentication=ActiveDirectoryMSI;`
105104
106105
# User-assigned managed identity connection string format
107-
# `Driver={ODBC Driver 17 for SQL Server};Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;UID=<msiClientId>;Authentication=ActiveDirectoryMSI;`
106+
# `Server=tcp:<Fabric-SQL-Identifier>.msit-database.fabric.microsoft.com,1433;Database=<SQL-DB-name>-<Fabric-DB-Identifier>;UID=<msiClientId>;Authentication=ActiveDirectoryMSI;`
108107
109-
conn = pyodbc.connect(connString)
108+
conn = connect(connection_string)
110109
```
111110
112111
### [Go](#tab/fabricsql-me-id-go)

articles/service-connector/includes/code-sql-me-id.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,33 @@ Update your application following the tutorial [Migrate a Java application to us
8282
8383
1. Install dependencies.
8484
```bash
85-
python -m pip install pyodbc
85+
python -m pip install mssql-python python-dotenv
8686
```
8787
88-
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. If you are using Azure Container Apps as compute service or the connection string in the code snippet doesn't work, refer to [Migrate a Python application to use passwordless connections with Azure SQL Database](/azure/azure-sql/database/azure-sql-passwordless-migration-python) to connect to Azure SQL Database using an access token.
88+
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
8989
9090
```python
9191
import os
92-
import pyodbc
92+
from mssql_python import connect
9393
9494
server = os.getenv('AZURE_SQL_SERVER')
9595
port = os.getenv('AZURE_SQL_PORT')
9696
database = os.getenv('AZURE_SQL_DATABASE')
97-
authentication = os.getenv('AZURE_SQL_AUTHENTICATION')
9897
9998
# Uncomment the following lines corresponding to the authentication type you want to use.
10099
# For system-assigned managed identity.
101-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
100+
# connection_string = f'Server={server},{port};Database={database};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
102101
103102
# For user-assigned managed identity.
104-
# clientID = os.getenv('AZURE_SQL_USER')
105-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={clientID};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
103+
# client_id = os.getenv('AZURE_SQL_USER')
104+
# connection_string = f'Server={server},{port};Database={database};UID={client_id};Authentication=ActiveDirectoryMSI;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
106105
107106
# For service principal.
108107
# user = os.getenv('AZURE_SQL_USER')
109108
# password = os.getenv('AZURE_SQL_PASSWORD')
110-
# connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},{port};Database={database};UID={user};PWD={password};Authentication={authentication};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
109+
# connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Authentication=ActiveDirectoryServicePrincipal;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
111110
112-
conn = pyodbc.connect(connString)
111+
conn = connect(connection_string)
113112
```
114113
115114
### [NodeJS](#tab/sql-me-id-nodejs)

articles/service-connector/includes/code-sql-secret.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,30 @@ ms.author: wchi
8181
8282
1. Install dependencies.
8383
```bash
84-
python -m pip install pyodbc
84+
python -m pip install mssql-python python-dotenv
8585
```
8686
8787
1. Get the Azure SQL Database connection configurations from the environment variable added by Service Connector.
8888
```python
89-
import os;
90-
import pyodbc
89+
import os
90+
from mssql_python import connect
9191
9292
server = os.getenv('AZURE_SQL_SERVER')
9393
port = os.getenv('AZURE_SQL_PORT')
9494
database = os.getenv('AZURE_SQL_DATABASE')
9595
user = os.getenv('AZURE_SQL_USER')
9696
password = os.getenv('AZURE_SQL_PASSWORD')
9797
98-
connString = f'Driver={{ODBC Driver 18 for SQL Server}};Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
98+
connection_string = f'Server={server},{port};Database={database};UID={user};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
9999
100-
conn = pyodbc.connect(connString)
100+
conn = connect(connection_string)
101101
```
102102
103103
### [Django](#tab/sql-secret-django)
104104
1. Install dependencies.
105105
```bash
106106
pip install django
107-
pip install pyodbc
107+
pip install mssql-django pyodbc
108108
```
109109
110110
1. In the setting file, get the Azure SQL Database connection configurations from the environment variable added by Service Connector.
@@ -119,14 +119,14 @@ ms.author: wchi
119119
120120
DATABASES = {
121121
'default': {
122-
'ENGINE': 'sql_server.pyodbc',
122+
'ENGINE': 'mssql',
123123
'NAME': database,
124124
'USER': user,
125125
'PASSWORD': password,
126126
'HOST': server,
127127
'PORT': port,
128128
'OPTIONS': {
129-
'driver': 'ODBC Driver 13 for SQL Server',
129+
'driver': 'ODBC Driver 18 for SQL Server',
130130
},
131131
},
132132
}

articles/storage/common/multiple-identity-scenarios.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ To configure this setup in your code, ensure your application registers separate
669669
1. In your project, install the required packages. The Azure Identity library provides `DefaultAzureCredential`.
670670
671671
```bash
672-
pip install azure-identity azure-storage-blob azure-cosmos pyodbc
672+
pip install azure-identity azure-storage-blob azure-cosmos mssql-python
673673
```
674674
675675
1. Add the following to your code:
@@ -678,7 +678,8 @@ To configure this setup in your code, ensure your application registers separate
678678
from azure.cosmos import CosmosClient
679679
from azure.identity import DefaultAzureCredential
680680
from azure.storage.blob import BlobServiceClient
681-
import os, pyodbc, struct
681+
from mssql_python import connect
682+
import os
682683
683684
# Create a DefaultAzureCredential instance that configures the underlying
684685
# ManagedIdentityCredential to use a user-assigned managed identity.
@@ -698,23 +699,21 @@ To configure this setup in your code, ensure your application registers separate
698699
credential=credential_storage
699700
)
700701
701-
# Create a DefaultAzureCredential instance that configures the underlying
702-
# ManagedIdentityCredential to use a user-assigned managed identity.
702+
# Create an Azure Cosmos DB client
703+
# Note: For Cosmos DB, we still need DefaultAzureCredential
703704
credential_databases = DefaultAzureCredential(
704705
managed_identity_client_id=os.environ['Managed_Identity_Client_ID_Databases']
705706
)
706707
707-
# Create an Azure Cosmos DB client
708708
cosmos_client = CosmosClient(
709709
os.environ['COSMOS_ENDPOINT'],
710710
credential=credential_databases
711711
)
712712
713-
# Connect to Azure SQL
714-
token_bytes = credential_databases.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
715-
token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
716-
SQL_COPT_SS_ACCESS_TOKEN = 1256 # This connection option is defined by microsoft in msodbcsql.h
717-
conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
713+
# Connect to Azure SQL using mssql-python with managed identity
714+
# The driver handles authentication internally when using ActiveDirectoryMSI
715+
connection_string = f"Server=<your-server>.database.windows.net;Database=<your-database>;UID={os.environ['Managed_Identity_Client_ID_Databases']};Authentication=ActiveDirectoryMSI;Encrypt=yes;"
716+
conn = connect(connection_string)
718717
```
719718
720719
---

0 commit comments

Comments
 (0)