|
1 | | -# Connect to SQL Database by using Python on Windows using pyodbc |
| 1 | +# Connect to SQL using Python on Windows |
2 | 2 |
|
| 3 | +Python sample code that runs on an Windowsclient computer to connect to an Azure SQL Database using the **pyodbc** connector. |
3 | 4 |
|
4 | | -[Python code sample] (sample_python_windows.py) that runs on a Windows computer. The sample and connects to Microsoft SQL Database by using the **pyodbc** driver. |
| 5 | +### Contents |
5 | 6 |
|
| 7 | +[About this sample](#about-this-sample)<br/> |
| 8 | +[Before you begin](#before-you-begin)<br/> |
| 9 | +[Run this sample](#run-this-sample)<br/> |
| 10 | +[Sample details](#sample-details)<br/> |
| 11 | +[Disclaimers](#disclaimers)<br/> |
| 12 | +[Related links](#related-links)<br/> |
6 | 13 |
|
7 | | -## Requirements |
8 | 14 |
|
| 15 | +<a name=about-this-sample></a> |
9 | 16 |
|
10 | | -- [Python 2.7.6](https://www.python.org/download/releases/2.7.6/). |
11 | | -- [ODBC Driver 11 or 13 for SQL Server Windows](https://www.microsoft.com/en-us/download/details.aspx?id=50420) |
12 | | -- [Pyodbc](https://pypi.python.org/pypi/pyodbc/3.0.10) |
| 17 | +## About this sample |
13 | 18 |
|
14 | | -### Install the required modules |
| 19 | +- **Applies to:** SQL Server 2016 (or higher), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse |
| 20 | +- **Workload:** CRUD |
| 21 | +- **Programming Language:** Python |
| 22 | +- **Authors:** Meet Bhagdev [meet-bhagdev] |
15 | 23 |
|
16 | | -Navigate to your Python directory where the pip installer is located. For example C:\Python27\Scripts>. |
| 24 | +<a name=before-you-begin></a> |
17 | 25 |
|
18 | | -####Install pyodbc |
| 26 | +## Before you begin |
19 | 27 |
|
| 28 | +To run this sample, you need the following prerequisites. |
| 29 | + |
| 30 | +**Software prerequisites:** |
| 31 | +1. SQL Server 2016 (or higher) or an Azure SQL Database |
| 32 | + |
| 33 | +2. Python |
| 34 | + - [Download from here](https://www.python.org/download/releases/2.7.6/) |
| 35 | + |
| 36 | +3. Microsoft ODBC Driver 11 or 13 for SQL Server Windows |
| 37 | + - [Download from here](https://www.microsoft.com/en-us/download/details.aspx?id=50420) |
| 38 | + |
| 39 | +4. [Pyodbc](https://pypi.python.org/pypi/pyodbc/3.0.10) |
| 40 | + |
| 41 | + ``` |
20 | 42 | pip install pyodbc |
| 43 | + ``` |
| 44 | +
|
| 45 | + *Note: Instructions to enable the use pip can be found [here](http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows)* |
| 46 | +
|
| 47 | +
|
| 48 | +**Azure prerequisites:** |
| 49 | +
|
| 50 | +1. An AdventureWorks sample database: |
| 51 | +
|
| 52 | + - 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](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) |
| 53 | + |
| 54 | +## Run this sample |
| 55 | +
|
| 56 | +1. From your terminal, update the connection string details in the Python file with your own username, password, and hostname. |
| 57 | +
|
| 58 | +2. Run the code sample by running the below in your terminal: |
21 | 59 |
|
22 | | -*Note: Instructions to enable the use pip can be found [here](http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows)* |
| 60 | + ``` |
| 61 | + python sample_python_windows.py |
| 62 | + ``` |
23 | 63 |
|
| 64 | +<a name=sample-details></a> |
24 | 65 |
|
25 | | -## Create a database and retrieve your connection string |
| 66 | +## Sample details |
26 | 67 |
|
27 | | -For simplicity we will create an Azure SQL Database for the sample. You can use the same sample for an on-premise SQL Server instance. See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an AdventureWorks database template. The samples shown below only work with the AdventureWorks schema. |
| 68 | +The above sample code just connected to your AdventureWorks database and performed a SELECT statement and an INSERT statement. |
28 | 69 |
|
29 | | -See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an **AdventureWorks database template**. The samples shown below only work with the **AdventureWorks schema**. |
| 70 | +## Disclaimers |
| 71 | +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. |
0 commit comments