mssql-python is the official Microsoft Python library for interacting with Microsoft SQL Server, Azure SQL, and SQL databases in Fabric. It provides high-performance data access, bulk copy operations, and Apache Arrow integration. Currently at version 1.5.0, the library maintains an active release cadence with regular enhancements and bug fixes.
pip install mssql-pythonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Microsoft SQL Server database using `mssql-python`, execute a simple query, create a table, insert data, and fetch results. It highlights the use of environment variables for secure credential management and the recommended `with` statement for connection and cursor management.
Before installing `mssql-python`, install the appropriate Microsoft ODBC Driver for SQL Server for your operating system. Refer to Microsoft's official documentation for installation instructions (e.g., for Ubuntu, macOS, Windows). Ensure the `DRIVER={...}` string in your connection matches the installed driver (e.g., `ODBC Driver 18 for SQL Server`).If manually setting encoding for wide character types, ensure you specify `utf-16le` or `utf-16be`. Avoid using generic `utf-16`.
For concurrent database operations, create a separate connection object for each thread or use a connection pooling library. Avoid sharing a single `mssql_python.connect` object across multiple threads without explicit locking mechanisms.
Familiarize yourself with the `cursor.bulkcopy()` documentation. Pay close attention to data type conversions, potential logging verbosity, and error handling, especially for large datasets. Ensure data provided matches target table schema.