1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/tutorials/python/pyodbc/windows

Connect to SQL using Python on Windows

Python sample code that runs on an Windowsclient computer to connect to an Azure SQL Database using the pyodbc connector.

Contents

About this sample
Before you begin
Run this sample
Sample details
Disclaimers
Related links

About this sample

  • Applies to: SQL Server 2016 (or higher), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse
  • Workload: CRUD
  • Programming Language: Python
  • Authors: Meet Bhagdev [meet-bhagdev]

Before you begin

To run this sample, you need the following prerequisites.

Software prerequisites:

  1. SQL Server 2016 (or higher) or an Azure SQL Database

  2. Python

  3. Microsoft ODBC Driver 11 or 13 for SQL Server Windows

  4. Pyodbc

    pip install pyodbc
    

    Note: Instructions to enable the use pip can be found here

Azure prerequisites:

  1. An AdventureWorks sample database:

    • The Python sample relies on the AdventureWorks sample database. If you do not already have AdventureWorks, you can see how to create it at the following topic: Create your first Azure SQL Database

Run this sample

  1. From your terminal, update the connection string details in the Python file with your own username, password, and hostname.

  2. Run the code sample by running the below in your terminal:

    python sample_python_windows.py
    

Sample details

The above sample code just connected to your AdventureWorks database and performed a SELECT statement and an INSERT statement.

Disclaimers

The scripts and this guide are copyright Microsoft Corporations and are provided as samples. They are not part of any Azure service and are not covered by any SLA or other Azure-related agreements. They are provided as-is with no warranties express or implied. Microsoft takes no responsibility for the use of the scripts or the accuracy of this document. Familiarize yourself with the scripts before using them.