Movatterモバイル変換


[0]ホーム

URL:


Connect to Azure SQL Database using Python and Jupyter Notebook

In this tutorial, you will learn how to use Python and Jupyter Notebooks to connect to an Azure SQL Database. To complete the exercise, you will need to install:

  • Python 3 (make sure to add Python to your PATH) and
  • Visual Studio Code.

Set up pyodbc Python development environment

In this exercise, you will use thepyodbc library to connect to your Azure SQL database and execute SQL queries.

  1. Install theMicrosoft ODBC Driver for SQL Server on Windows.

  2. Open the command prompt and run the following command to install the pyodbc library.

    1
    pip install pyodbc

Set up Jupyter Notebooks development environment

  1. Open the command prompt and run the following command to install the Jupyter Notebook.

    1
    pip install jupyter
  2. Then, open Visual Studio Code. Go toView > Extensions and search forJupyter.

  3. Install theJupyter Extension for Visual Studio Code.

Get connection information

The next step is to collect the information that you need to connect to the Azure SQL Database. You need the server name, the database name, and the login credentials (username and password).

  1. Sign in to Azure Portal, expand the left navigation panel and selectAll resources.

    Select All resources in Azure
  2. Select the SQL Database that you have created in thepart 1.

  3. In theOverview tab, you can see the fully qualified server name.

    Server name - Azure portal

Create Connection

Open Visual Studio Code and create a new Jupyter file namedconnect-sql.ipynb. In the first cell, add the following code to connect to your sql database using Python:

 1 2 3 4 5 6 7 8 9101112
importpyodbc# Establish the connectionserver='<server>.database.windows.net'database='<database>'username='<username>'password='<password>'driver='{ODBC Driver 17 for SQL Server}'conn= pyodbc.connect('DRIVER='+ driver+';SERVER='+    server+';PORT=1433;DATABASE='+ database+';UID='+ username+';PWD='+ password)print(conn)conn.close()

where:

  • server is the server name,
  • database refers to the name of the database that you created in part 1,
  • username is the admin login name, and
  • password is the password of the admin.

Next steps

Now you are ready to start working with Azure SQL databases in Python. In the following tutorials, you will learn how to insert a Python dataframe into a SQL table and manipulate data using Python and SQL statements in Jupyter Notebooks.

You May Also Like

Manipulate data in Azure SQL Database using Python and Jupyter Notebook

Manipulate data in Azure SQL Database using Python and Jupyter Notebook

This tutorial will show you how to manipulate and query data in an Azure SQL database using Python and Jupyter Notebooks.

Insert Python dataframe into Azure SQL table

Insert Python dataframe into Azure SQL table

In this tutorial, you will learn how to use Python and Jupyter Notebooks to insert a dataframe into an Azure SQL table.

Global Azure 2021 – Azure SQL Database: Use Python to manipulate data

Global Azure 2021 – Azure SQL Database: Use Python to manipulate data

The session “Azure SQL Database: Use Python to manipulate data” at Global Azure Greece 2021 by Foteini Savvidou.

Create an Azure SQL Database via the Azure portal

Create an Azure SQL Database via the Azure portal

In this tutorial, you will learn how to create an Azure SQL Database and configure firewall rules to enable connectivity.


[8]ページ先頭

©2009-2025 Movatter.jp