Learn to Connect SQL Server Database with Python using JupyterLab
Prerequisites:
- Installation of Python 13 or above.
- Installation of JupyterLab on Windows Machine.
- Installation of SQL Server Database 2019 or above
- Knowledge of SQL Server Database.
- 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.
!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.