Quickstart: Use Python to query a database in Azure SQL Database or Azure SQL Managed Instance

APPLIES TO: Azure SQL Database Azure SQL Managed Instance

In this quickstart, you use Python to connect to Azure SQL Database or Azure SQL Managed Instance, and use T-SQL statements to query data.


To complete this quickstart, you need:


The scripts in this article are written to use the Adventure Works database.


You can optionally choose to use an Azure SQL Managed Instance.

To create and configure, use the Azure portal, PowerShell, or the CLI, and then set up on-premises or VM connectivity.

To load data, see restore with BACPAC with the Adventure Works file, or see restore the Wide World Importers database.

To further explore Python and the database in Azure SQL Database, see Azure SQL Database libraries for Python, the pyodbc repository, and a pyodbc sample.

Get server connection information

Get the connection information you need to connect to the database in Azure SQL Database. You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.

  1. Sign in to the Azure portal.

  2. Go to the SQL Databases or SQL Managed Instances page.

  3. On the Overview page, review the fully qualified server name next to Server name for the database in Azure SQL Database or the fully qualified server name (or IP address) next to Host for an Azure SQL Managed Instance or SQL Server on Azure VM. To copy the server name or host name, hover over it and select the Copy icon.


For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.

Create code to query your database

  1. In a text editor, create a new file named sqltest.py.

  2. Add the following code. Substitute your own values for <server>, <database>, <username>, and <password>.


    The code in this example uses the sample AdventureWorksLT data, which you can choose as source when creating your database. If your database has different data, use tables from your own database in the SELECT query.

    import pyodbc
    server = '<server>.database.windows.net'
    database = '<database>'
    username = '<username>'
    password = '<password>'   
    driver= '{ODBC Driver 17 for SQL Server}'
    with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
            row = cursor.fetchone()
            while row:
                print (str(row[0]) + " " + str(row[1]))
                row = cursor.fetchone()

Run the code

  1. At a command prompt, run the following command:

    python sqltest.py
  2. Verify that the top 20 Category/Product rows are returned, and then close the command window.

Next steps