Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

microsoft/mssql-python

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.

Installation

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

Key Features

Supported Platforms

Windows, MacOS and Linux (manylinux2014 - Debian, Ubuntu & RHEL)

Note:Support for additional Linux OSs (Alpine, SUSE Linux) will come soon

DBAPI v2.0 Compliance

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.

Support for Microsoft Entra ID Authentication

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 MethodWindows SupportmacOS/Linux SupportNotes
ActiveDirectoryPassword✅ Yes✅ YesUsername/password-based authentication
ActiveDirectoryInteractive✅ Yes✅ YesInteractive login via browser; requires user interaction
ActiveDirectoryMSI (Managed Identity)✅ Yes✅ YesFor Azure VMs/containers with managed identity
ActiveDirectoryServicePrincipal✅ Yes✅ YesUse client ID and secret or certificate
ActiveDirectoryIntegrated✅ Yes❌ NoOnly works on Windows (requires Kerberos/SSPI)
ActiveDirectoryDeviceCode✅ Yes✅ YesDevice code flow for authentication; suitable for environments without browser access
ActiveDirectoryDefault✅ Yes✅ YesUses default authentication method based on environment and configuration

NOTE:

  • Access Token: the connection stringmust not containUID,PWD,Authentication, orTrusted_Connection keywords.
  • Device Code: make sure to specify aConnect 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.

Enhanced Pythonic Features

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.

Connection Pooling

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.

Getting Started Examples

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()

Still have questions?

Check out ourFAQ. Still not answered? Create anissue to ask a question.

Contributing

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.

License

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.

Trademarks

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

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors11


[8]ページ先頭

©2009-2025 Movatter.jp