This article will discuss the quick steps to connect to the Azure SQL database using Python.
How to connect to Azure SQL database using Python
You can easily connect to the Azure SQL database using Python using the instructions below.
As a prerequisite, you need the below stuff
- You must have an Azure Account. You can create an Azure Free account now if you don’t have an Azure Account.
- You must have a database.
- You must install Python and the other dependencies.
Open your favorite text editor and name it test.py.
Now add the below script to that file.
import pyodbc
myserver = '<yourservername>.database.windows.net'
databasenm = '<yourdatabasename>'
username = '<username>'
password = '<password>'
driverdetails= '{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driverdetails+';SERVER='+myserver+';PORT=1433;DATABASE='+databasenm+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 4 name FROM sys.databases")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()- Now, open the command prompt and run the below command to run the above script
python test.pyYou may also like following the articles below
Conclusion
This article discussed how to connect to the Azure SQL database using Python. Thanks for reading this article !!!

I am Rajkishore, and I am a Microsoft Certified IT Consultant. I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machines, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more.
