Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
In this quickstart, you use Python to connect to Azure SQL Database, Azure SQL Managed Instance, or Synapse SQL database and use T-SQL statements to query data.
mssql-python documentation | mssql-python source code | Package (PyPi)
Prerequisites
To complete this quickstart, you need:
An Azure account with an active subscription. Create a Trial Subscription.
A database
You can use one of these quickstarts to create and then configure a database:
Action SQL Database SQL Managed Instance SQL Server on Azure VM Azure Synapse Analytics Create Portal Portal Portal Portal CLI CLI PowerShell PowerShell PowerShell PowerShell Deployment template Deployment template Deployment template Configure Server-level IP firewall rule Connectivity from a VM Connectivity settings Connectivity from on-premises Connect to a SQL Server instance Get connection information Azure SQL Azure SQL SQL VM Synapse SQL Python 3
If you don't already have Python, install the Python runtime and Python Package Index (PyPI) package manager from python.org.
Prefer to not use your own environment? Open as a devcontainer using GitHub Codespaces.
A database on SQL Server, or Azure SQL Database with the AdventureWorks2022 sample schema and a valid connection string.
Setting up
Follow these steps to configure your development environment to develop an application using the mssql-python
Python driver.
Note
This driver uses the Tabular Data Stream (TDS) protocol, which is enabled by default in SQL Serve, and Azure SQL Database. No extra configuration is required.
Install the mssql-python package
Get the mssql-python
package from PyPI.
Open a command prompt in an empty directory.
Install the
mssql-python
package.
Install python-dotenv package
Get the python-dotenv
from PyPI.
In the same directory, install the
python-dotenv
package.pip install python-dotenv
Check installed packages
You can use the PyPI command-line tool to verify that your intended packages are installed.
Check the list of installed packages with
pip list
.pip list
Create new files
In the current directory, create a new file named
.env
.Within the
.env
file, add an entry for your connection string namedSQL_CONNECTION_STRING
. Replace the example here with your actual connection string value.SQL_CONNECTION_STRING="Server=<server_name>;Database={<database_name>};Encrypt=yes;TrustServerCertificate=no;Authentication=ActiveDirectoryInteractive"
Tip
The connection string used here largely depends on the type of SQL database you're connecting to. If you're connecting to an Azure SQL Database, use the ODBC connection string from the connection strings tab. You might need to adjust the authentication type depending on your scenario. For more information on connection strings and their syntax, see DSN and Connection String Keywords and Attributes.
In a text editor, create a new file named sqltest.py.
Add the following code.
from os import getenv from dotenv import load_dotenv from mssql_python import connect load_dotenv() with connect(getenv("SQL_CONNECTION_STRING")) as conn: with conn.cursor() as cursor: cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases") rows = cursor.fetchall() for row in rows: print(row.name, row.collation_name)
Run the code
At a command prompt, run the following command:
python sqltest.py
Verify that the databases and their collations are returned, and then close the command window.
If you receive an error:
Verify that the server name, database name, username, and password you're using are correct.
If you're running the code from a local environment, verify that the firewall of the Azure resource you're trying to access is configured to allow access from your environment's IP address.