Connect to SQL using Python on Ubuntu Linux
Python sample code that runs on an Ubuntu Linux client 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.
-
SQL Server 2016 (or higher) or an Azure SQL Database
-
Python
sudo apt-get --assume-yes update sudo apt-get --assume-yes install python-dev python-pip -
Microsoft ODBC Driver for SQL Server
sudo su wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh sh installodbc.sh -
pyodc
sudo -H pip install pyodbc
Azure prerequisites:
-
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
-
From your terminal, update the connection string details in the Python file with your own username, password, and hostname.
-
Run the code sample by running the below in your terminal:
python sample_python_linux.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.