Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
Ray_Minds
Continued Contributor
Continued Contributor

SQL connection with Python

Using DB objects from Microsoft SQL Server DB in Python

 

 We will be covering the installation of libraries, creating a parameter, and building connection for SQL server in Python (jupyter notebook). The following can also be used to run Python code: PyCharm, Visual Studio & Anaconda etc as per your convenience. Pre-requisites:

 
  • Install 3.0x Python on your machine.

  • Microsoft SQL Server ODBC driver installed.

  • Install the pyodbc library.

 

Installation:

 
  1. Install the ‘pyodbc’ library in the jupyter notebook, please use the code below.  # pip install pyodbc

  2. If already installed, you will get a message as a snapshot below.

 
Ray_Minds_0-1726565884127.png

 

 
 
 

Connecting to Microsoft SQL Server Using Windows Authentication:

 

1. Now, we need to import the library, please use the below code. # import ‘pyodbc

 
 
 
Ray_Minds_1-1726565884060.png

 

 

2. Create a parameter to build a connection, please use the code below. #server_name = "server_name"; # db = “database_name”; # driver = “{ODBC Driver 18 for SQL Server}”;  Note: - Please make sure which driver version installed on your machine. 3. Build the connection to the Microsoft SQL Server using window authentication with the help of the above parameter, please use the code below.

 

# ‘conn=pyodbc.connect(

 

                               f'DRIVER={driver};'

 

                               f'SERVER={server_name};

 

                              'f'DATABASE={db};

 

                              'f'Trusted_Connection=yes; )

 

 

 

4. Please make sure your connection builds successfully using the print function , by using the below code.

 

# ‘print(conn)’

 
 
 

5. Create the cursor object & make sure the cursor connection build is successful. please use the below code. # cursor = conn.cursor # print(cursor) 

 
 
 

6. Prepare and execute the query, please use the code below. # cursor.execute(“Query Statement”). 

 
 
 

Note:- you will be using your own query.

 

7. Now, we need to fetch the query and then process the result, please use the code below. # result = cursor.fetchall() # print(result)

 
 

8. Finally, we need to close the open session for cursor & connection, please use the code below. # cursor.close() # conn.close()

 
 

Summary: Connecting to Microsoft SQL Server using Python and pyodbc library provides flexibility, automation, and cross-platform compatibility, making it an excellent choice for integrating database operations into Python applications. While SSMS provides a user-friendly GUI and is fine-tuned for database management tasks with limited functionality, pyodbc allows for more flexible and automated solutions suitable for various development, data processing, and data transformation.


 
For more information and insights, feel free to visit my blog https://www.rayminds.com/post/sql-connection-with-python
. I regularly share updates across different communities, so stay tuned for more content!

 

1 REPLY 1
Anonymous
Not applicable

Hi @Ray_Minds ,

 

Thank you for sharing this content, it is very helpful to our community.

 

We are sure your post will make more people aware of how to build connections for SQL Server in Python.

 

You may consider accepting my reply as a solution, which will allow more people to see your post and thus help the community even more.

 

Thanks again for your contribution to the community.

 

I wish you a wonderful day.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.