Python SQL Server

This tutorial series shows you how to work with the SQL Server from Python using the pymssql package.

Python Drivers for SQL Server

To connect to the SQL Server from Python, you need a driver. There are some Python SQL Server drivers available.

Here are the drivers recommended by Microsoft:

  • pyodbc – provides an interface for connecting to SQL Server databases via ODBC interface, allowing you to execute SQL queries seamlessly.
  • pymssql – is a Python library designed for connecting to SQL Server databases and managing data efficiently.

We’ll use the pymssql package to connect to the SQL Server from Python.

The pymssql Package Features

The following highlights some features of the pymssql package:

  • Python 3: Utilize the advantage of the latest features and improvements in Python 3.
  • Unicode: Enable handling international characters.
  • Works across popular operating systems (OS): Support the most popular OSes including Windows, macOS, or Linux.
  • Written in Cython for performance: Benefit from faster execution and optimized performance.
  • Supports stored procedures: Call SQL Server’s stored procedures with return values or output parameters.
  • Supports bulk copy: Quickly and efficiently transfer large amounts of data to SQL Server.
  • Can be used to connect to SQL Server on Azure: Seamlessly integrate with Microsoft’s cloud platform for scalable and reliable database solutions.

Section 1. Getting Started

Section 2. Inserting Data

  • Insert data into a table – show you how to insert data into a table in SQL Server from Python
  • Import a CSV file into a table – learn how to develop a Python program that reads data from a CSV file and imports it into a table in the SQL Server.
  • Bulk Copy – discover how to quickly load data into an SQL Server table using the bulk copy function.

Section 3. Updating Data

Section 4. Querying Data

  • Select data from a table – Guide you on how to select one or more rows from a table in Python
  • Pagination – Show you how to break up a large result set into smaller chunks (pages) using FETCH and OFFSET clauses.

Section 5. Deleting Data

Section 6. Handling Transactions

Section 7. Calling Stored Procedures

Was this tutorial helpful?