Header Ads

Learn to Connect SQL Server Database with Python using JupyterLab

In today's world of data, if you are not familiar with python or any other data analytical language, then you are surely missing out on a lot of fun. While python provides some very powerful libraries to perform or build data analysis, machine learning or data science queries or solutions. But, for data processing or engineering, database engines like SQL server, Postgress or MySql still hold the power over python. Processing data directly within python may lead to performance overheads for complex or large datasets. So, it is highly recommended to process the data within database engines of your choice. This means you need to know how to connect your selected database engine with python to perform require tasks.

Today, I shall be demonstrating a solution to connect SQL server database with python code using JupyterLab on the local windows machine and then I shall showcase how to read data from SQL server database using python coding. 




Prerequisites:

Before proceeding any further in this article, following are some of the many prerequisites for this article:
  1. Installation of Python 13 or above. 
  2. Installation of JupyterLab on Windows Machine.
  3. Installation of SQL Server Database 2019 or above 
  4. Knowledge of SQL Server Database.
  5. Knowledge of Python.

The running working source code solution for this article is being developed in Python 13 JupyterLab Notebook on Windows with Microsoft SQL Server 2019 Professional as database. Provided source code contains SQL server example database along with the additional python codes for data insertion, update and deletion within SQL server database using python coding, which is not provided in the article. For the source code click on the download button below. 

Download Now!


Let's begin now.

1) First you need to successfully Install JupyterLab on the windows machine with python 13 or above as I have developed my solution with python 13.

 
2) Then you need to install Microsoft SQL Server 2019 or above on your machine.
 
 
3) Next run the JupyterLab tool to start the coding.


4) If you have the provided source code with you then you can copy the notebook file into "workspace" folder according to step-1 of JupyterLab installation directory structure. Otherwise create a new notebook.
 
 
5) In order to connect SQL server database with python install "pyodbc" library. Also, install "pandas" library if not already installed and then import them i.e.

!pip install pandas
!pip install pyodbc


import pyodbc as odbc
import pandas as pd

6) Next step is to make SQL server database connection with python using following line of code. I have provided an example customer database in the source code of this article. Make sure to change server name, database name, username and password in below line of code with your own SQL server i.e.

# Initialize SQL Database Connection.
conn = odbc.connect('DRIVER={SQL Server};SERVER=YOUR_SQL_SERVER_NAME;DATABASE=YOUR_SQL_DATABASE_NAME;UID=YOUR_SQL_USERNAME;PWD=YOUR_SQL_PASSWORD;')

7) Now that SQL server connection is made, it's time to display list of data available in SQL server database customers table within python using pandas library dataframe i.e. 

# Select All data.
query = "SELECT * FROM tbl_customers"
data = pd.read_sql_query(query, conn)

# Output
data

# Select Specific data.
query = "SELECT * FROM tbl_customers WHERE first_name = 'Lucy'"
data = pd.read_sql_query(query, conn)

# Output
data

The above select queries will display all customers and specific customer data within python output cell as a dataframe. I recommend to split the above code into two separate cells.
 


  
8) If you are interested in learning how to use SQL server database insert, update and delete queries from within python code then I recommend to get the provided source code of this article.





Conclusion

In this article, you will learn to connect SQL server database with python code using JupyterLab on the local windows machine. You will also learn to read data from SQL server database with SELECT query using python coding. Finally, if you are interested in learning about how to use SQL server database insert, update and delete queries from within python code, then I recommend to get the provided source code of this article.

Related Articles