|
| 1 | +# Update Python code samples to use mssql-python instead of pyodbc |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +This PR updates all Python code samples in azure-docs-pr to use **mssql-python**, Microsoft's official Python driver for SQL Server, replacing the legacy pyodbc approach. |
| 6 | + |
| 7 | +## Why this change? |
| 8 | + |
| 9 | +**mssql-python** is Microsoft's new official Python driver for SQL Server that offers significant advantages: |
| 10 | + |
| 11 | + |
| 12 | +### Key Benefits |
| 13 | + |
| 14 | +- **No ODBC Driver installation** - mssql-python includes its own native implementation |
| 15 | +- **Built-in Microsoft Entra authentication** - No need for azure-identity + manual token handling |
| 16 | +- **Simpler installation** - Just `pip install mssql-python` |
| 17 | +- **Cross-platform support** - Windows, macOS, and Linux (some Linux distros need standard system libs) |
| 18 | + |
| 19 | +## Changes Made |
| 20 | + |
| 21 | +### Files Updated (7 files) |
| 22 | + |
| 23 | +| File | Description | |
| 24 | +|------|-------------| |
| 25 | +| `articles/service-connector/includes/code-sql-secret.md` | Secret/password authentication sample | |
| 26 | +| `articles/service-connector/includes/code-sql-me-id.md` | Microsoft Entra ID authentication sample | |
| 27 | +| `articles/service-connector/includes/code-fabricsql-me-id.md` | SQL database in Fabric managed identity sample | |
| 28 | +| `articles/app-service/includes/tutorial-connect-msi-azure-database/code-sql-mi.md` | App Service managed identity sample | |
| 29 | +| `articles/storage/common/multiple-identity-scenarios.md` | Multiple identity scenarios sample | |
| 30 | +| `articles/app-service/troubleshoot-intermittent-outbound-connection-errors.md` | Updated reference link | |
| 31 | +| `articles/azure-functions/recover-python-functions.md` | Added tip recommending mssql-python | |
| 32 | + |
| 33 | +### Code Pattern Changes |
| 34 | + |
| 35 | +**Before (pyodbc):** |
| 36 | +```python |
| 37 | +import pyodbc |
| 38 | +from azure.identity import DefaultAzureCredential |
| 39 | +import struct |
| 40 | + |
| 41 | +# Manual token acquisition required |
| 42 | +credential = DefaultAzureCredential() |
| 43 | +token = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE") |
| 44 | +token_struct = struct.pack(f'<I{len(token)}s', len(token), token) |
| 45 | +SQL_COPT_SS_ACCESS_TOKEN = 1256 |
| 46 | + |
| 47 | +# Requires ODBC Driver to be installed on the system |
| 48 | +conn_string = "Driver={ODBC Driver 18 for SQL Server};Server=server.database.windows.net;Database=mydb;" |
| 49 | +conn = pyodbc.connect(conn_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct}) |
| 50 | +``` |
| 51 | + |
| 52 | +**After (mssql-python):** |
| 53 | +```python |
| 54 | +from mssql_python import connect |
| 55 | + |
| 56 | +# No ODBC driver needed, no manual token handling |
| 57 | +connection_string = "Server=server.database.windows.net;Database=mydb;Authentication=ActiveDirectoryDefault;Encrypt=yes;" |
| 58 | +conn = connect(connection_string) |
| 59 | +``` |
| 60 | + |
| 61 | +### Authentication Methods Supported |
| 62 | + |
| 63 | +| Method | Use Case | |
| 64 | +|--------|----------| |
| 65 | +| `ActiveDirectoryDefault` | Local dev (uses `az login` credentials) | |
| 66 | +| `ActiveDirectoryMSI` | Azure hosted (App Service, Functions, VMs) | |
| 67 | +| `ActiveDirectoryInteractive` | Interactive browser login | |
| 68 | +| `ActiveDirectoryServicePrincipal` | Service principal with client ID/secret | |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +All code samples have been validated: |
| 73 | + |
| 74 | +- ✅ Python syntax validation passed for all 7 code blocks |
| 75 | +- ✅ mssql-python import verified |
| 76 | +- ✅ Live connection test to SQL database in Fabric successful |
| 77 | +- ✅ `ActiveDirectoryDefault` authentication tested |
| 78 | +- ✅ `ActiveDirectoryInteractive` authentication tested |
| 79 | + |
| 80 | +## References |
| 81 | + |
| 82 | +- [mssql-python GitHub](https://github.com/microsoft/mssql-python) |
| 83 | +- [Microsoft Entra ID authentication wiki](https://github.com/microsoft/mssql-python/wiki/Microsoft-Entra-ID-support) |
| 84 | +- [Installation guide](https://github.com/microsoft/mssql-python/wiki/Installation) |
0 commit comments