- Notifications
You must be signed in to change notification settings - Fork10
License
microsoft/mssql-python
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
mssql-python is a Python driver for Microsoft SQL Server and the Azure SQL family of databases. It leverages Direct Database Connectivity (DDBC) that enables direct connections to SQL Server without requiring an external driver manager. Designed to comply with theDB API 2.0 specification, this driver also introduces Pythonic enhancements for improved usability and functionality. It supports a full range of database operations, including connection management, query execution, and transaction handling.
The driver is compatible with all the Python versions >= 3.10
Documentation |Release Notes |Roadmap
Note:This project is currently in Public Preview, meaning it is still under active development. We are working on core functionalities and gathering more feedback before GA. Please use with caution and avoid production environments.
Windows: mssql-python can be installed withpip
pip install mssql-python
MacOS: mssql-python can be installed withpip
brew install opensslpip install mssql-python
Linux: mssql-python can be installed withpip
pip install mssql-python
Windows, MacOS and Linux (manylinux2014 - Debian, Ubuntu & RHEL)
Note:Support for additional Linux OSs (Alpine, SUSE Linux) will come soon
The Microsoftmssql-python module is designed to be fully compliant with the DB API 2.0 specification. This ensures that the driver adheres to a standardized interface for database access in Python, providing consistency and reliability across different database systems. Key aspects of DBAPI v2.0 compliance include:
- Connection Objects: Establishing and managing connections to the database.
- Cursor Objects: Executing SQL commands and retrieving results.
- Transaction Management: Supporting commit and rollback operations to ensure data integrity.
- Error Handling: Providing a consistent set of exceptions for handling database errors.
- Parameter Substitution: Allowing the use of placeholders in SQL queries to prevent SQL injection attacks.
By adhering to the DB API 2.0 specification, the mssql-python module ensures compatibility with a wide range of Python applications and frameworks, making it a versatile choice for developers working with Microsoft SQL Server, Azure SQL Database, and Azure SQL Managed Instance.
The Microsoft mssql-python driver enables Python applications to connect to Microsoft SQL Server, Azure SQL Database, or Azure SQL Managed Instance using Microsoft Entra ID identities. It supports a variety of authentication methods, including username and password, Microsoft Entra managed identity (system-assigned and user-assigned), Integrated Windows Authentication in a federated, domain-joined environment, interactive authentication via browser, device code flow for environments without browser access, and the default authentication method based on environment and configuration. This flexibility allows developers to choose the most suitable authentication approach for their deployment scenario.
EntraID authentication is now fully supported on MacOS and Linux but with certain limitations as mentioned in the table:
Authentication Method | Windows Support | macOS/Linux Support | Notes |
---|---|---|---|
ActiveDirectoryPassword | ✅ Yes | ✅ Yes | Username/password-based authentication |
ActiveDirectoryInteractive | ✅ Yes | ✅ Yes | Interactive login via browser; requires user interaction |
ActiveDirectoryMSI (Managed Identity) | ✅ Yes | ✅ Yes | For Azure VMs/containers with managed identity |
ActiveDirectoryServicePrincipal | ✅ Yes | ✅ Yes | Use client ID and secret or certificate |
ActiveDirectoryIntegrated | ✅ Yes | ❌ No | Only works on Windows (requires Kerberos/SSPI) |
ActiveDirectoryDeviceCode | ✅ Yes | ✅ Yes | Device code flow for authentication; suitable for environments without browser access |
ActiveDirectoryDefault | ✅ Yes | ✅ Yes | Uses default authentication method based on environment and configuration |
NOTE:
- Access Token: the connection stringmust not contain
UID
,PWD
,Authentication
, orTrusted_Connection
keywords. - Device Code: make sure to specify a
Connect Timeout
that provides enough time to go through the device code flow authentication process. - Default: Ensure you're authenticated via az login, or running within a managed identity-enabled environment.
The driver offers a suite of Pythonic enhancements that streamline database interactions, making it easier for developers to execute queries, manage connections, and handle data more efficiently.
The Microsoft mssql_python driver provides built-in support for connection pooling, which helps improve performance and scalability by reusing active database connections instead of creating a new connection for every request. This feature is enabled by default. For more information, referConnection Pooling Wiki.
Connect to SQL Server and execute a simple query:
importmssql_python# Establish a connection# Specify connection stringconnection_string="SERVER=<your_server_name>;DATABASE=<your_database_name>;UID=<your_user_name>;PWD=<your_password>;Encrypt=yes;"connection=mssql_python.connect(connection_string)# Execute a querycursor=connection.cursor()cursor.execute("SELECT * from customer")rows=cursor.fetchall()forrowinrows:print(row)# Close the connectionconnection.close()
Check out ourFAQ. Still not answered? Create anissue to ask a question.
This project welcomes contributions and suggestions. Most contributions require you to agree to aContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant usthe rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to providea CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructionsprovided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted theMicrosoft Open Source Code of Conduct.For more information see theCode of Conduct FAQ orcontactopencode@microsoft.com with any additional questions or comments.
The mssql-python driver for SQL Server is licensed under the MIT license, except the dynamic-link libraries (DLLs) in thelibs folderthat are licensed under MICROSOFT SOFTWARE LICENSE TERMS.
Please review theLICENSE file for more details.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsofttrademarks or logos is subject to and must followMicrosoft's Trademark & Brand Guidelines.Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.Any use of third-party trademarks or logos are subject to those third-party's policies.
About
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.