Skip to content

Commit 69f75d8

Browse files
committed
Updated pyodbc guide
1 parent b0da7fb commit 69f75d8

7 files changed

Lines changed: 318 additions & 121 deletions

docs/connect/python/pymssql/python-sql-driver-pymssql.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,29 @@ ms.topic: conceptual
1111

1212
# Python SQL Driver - pymssql
1313

14-
[!INCLUDE[Driver_Python_Download](../../../includes/driver_python_download.md)]
14+
Use the `pymssql` driver to connect to an SQL database from Python code. This series of articles provides step-by-step guidance for installing and using this Python SQL driver.
1515

1616
## Get started
1717

18-
* [Step 1: Configure development environment for pymssql Python development](step-1-configure-development-environment-for-pymssql-python-development.md)
19-
* [Step 2: Create an SQL database for pymssql Python development](step-2-create-a-sql-database-for-pymssql-python-development.md)
20-
* [Step 3: Proof of concept connecting to SQL using pymssql](step-3-proof-of-concept-connecting-to-sql-using-pymssql.md)
18+
- [Step 1: Configure development environment for pymssql Python development](step-1-configure-development-environment-for-pymssql-python-development.md)
19+
- [Step 2: Create an SQL database for pymssql Python development](step-2-create-a-sql-database-for-pymssql-python-development.md)
20+
- [Step 3: Proof of concept connecting to SQL using pymssql](step-3-proof-of-concept-connecting-to-sql-using-pymssql.md)
2121

2222
## Documentation
2323

24-
* [pymssql documentation](https://pypi.org/project/pymssql/)
24+
- [pymssql documentation](https://pypi.org/project/pymssql/)
2525

2626
## Support
2727

2828
Pymssql is community-supported software. This software doesn't come with Microsoft support.
2929

30-
To get help, file an [issue in the pymssql GitHub repository](https://github.com/pymssql/pymssql/issues) or visit other Python community resources.
30+
> [!TIP]
31+
> To get help, file an [issue in the pymssql GitHub repository](https://github.com/pymssql/pymssql/issues) or visit other Python community resources.
3132
3233
## Community resources
3334

34-
* [Azure Python Developer Center](https://azure.microsoft.com/develop/python/)
35-
* [python.org Community](https://www.python.org/community/)
35+
- [Azure Python Developer Center](https://azure.microsoft.com/develop/python/)
36+
- [python.org Community](https://www.python.org/community/)
3637

3738
## Next steps
3839

docs/connect/python/pymssql/step-1-configure-development-environment-for-pymssql-python-development.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ ms.topic: how-to
1212

1313
# Step 1: Configure development environment for pymssql Python development
1414

15-
You need to configure your development environment with the prerequisites in order to develop an application using the Python Driver for SQL Server. The Python SQL Drivers use the TDS protocol, which is enabled by default in SQL Server and Azure SQL Database. No extra configuration is required.
15+
You need to configure your development environment with the prerequisites in order to develop an application using the pymssql Python driver for SQL Server.
16+
17+
> [!NOTE]
18+
> This driver uses the TDS protocol, which is enabled by default in SQL Server and Azure SQL Database. No extra configuration is required.
1619
1720
## Prerequisites
1821

docs/connect/python/pymssql/step-3-proof-of-concept-connecting-to-sql-using-pymssql.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ Connect to a database using your credentials.
3232

3333
1. Create a new file named **app.py**.
3434

35+
1. Add a module docstring.
36+
37+
```python
38+
"""
39+
Connects to a SQL database using pymssql
40+
"""
41+
```
42+
3543
1. Import the `pymssql` package.
3644

3745
```python
@@ -81,7 +89,7 @@ Use an SQL query string to execute a query and parse the results.
8189
```
8290

8391
> [!NOTE]
84-
> This function essentially accepts any query and returns a result set, which can be iterated over with the use of [cursor.fetchone()](https://pypi.org/project/pymssql/).
92+
> This function essentially accepts any query and returns a result set, which can be iterated over with the use of [cursor.fetchone()]().
8593

8694
1. Use [`cursor.fetchall`](https://pymssql.readthedocs.io/en/latest/ref/pymssql.html#pymssql.Cursor.fetchall) with a `foreach` loop to get all the records from the database. Then print the records.
8795

@@ -91,7 +99,9 @@ Use an SQL query string to execute a query and parse the results.
9199
print(f"{r['CustomerID']}\t{r['OrderCount']}\t{r['CompanyName']}")
92100
```
93101

94-
1. Test the application.
102+
1. **Save** the **app.py** file.
103+
104+
1. Open a terminal and test the application.
95105

96106
```bash
97107
python app.py
@@ -121,7 +131,7 @@ In this example, you execute an [`INSERT`](../../../t-sql/statements/insert-tran
121131
productNumber = randrange(1000)
122132
```
123133

124-
> [!INFORMATION]
134+
> [!TIP]
125135
> Generating a random product number here ensures that you can run this sample multiple times.
126136

127137
1. Create a SQL statement string.
@@ -161,13 +171,26 @@ In this example, you execute an [`INSERT`](../../../t-sql/statements/insert-tran
161171
conn.commit()
162172
```
163173

174+
> [!TIP]
175+
> Optionally, you can use [`connection.rollback`](https://pymssql.readthedocs.io/en/latest/ref/pymssql.html#pymssql.Connection.rollback) to rollback the transaction.
176+
177+
1. Close the cursor and connection using [`cursor.close`](https://pymssql.readthedocs.io/en/latest/ref/pymssql.html#pymssql.Cursor.close) and [`connection.close`](https://pymssql.readthedocs.io/en/latest/ref/pymssql.html#pymssql.Connection.close).
178+
179+
```python
180+
cursor.close()
181+
conn.close()
182+
```
183+
184+
1. **Save** the **app.py** file and test the application again
185+
186+
```bash
187+
python app.py
188+
```
189+
164190
```output
165191
Inserted Product ID : 1001
166192
```
167193

168-
> [!TIP]
169-
> Optionally, you can use [`connection.rollback`](https://pymssql.readthedocs.io/en/latest/ref/pymssql.html#pymssql.Connection.rollback) to rollback the transaction.
170-
171194
## Next steps
172195

173196
> [!div class="nextstepaction"]

docs/connect/python/pyodbc/python-sql-driver-pyodbc.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@ ms.service: sql
88
ms.subservice: connectivity
99
ms.topic: conceptual
1010
---
11-
# Python SQL Driver - pyodbc
1211

13-
:::image type="icon" source="../../../includes/media/download.svg" border="false"::: [Install SQL driver for Python](../../sql-connection-libraries.md#anchor-20-drivers-relational-access)
12+
# Python SQL Driver - pyodbc
1413

15-
This article provides step-by-step guidance for installing and using the Python SQL Driver, pyODBC.
14+
Use the `pyodbc` driver to connect to an SQL database from Python code. This series of articles provides step-by-step guidance for installing and using this Python SQL driver.
1615

1716
## Get started
1817

19-
* [Step 1: Configure development environment for pyodbc Python development](step-1-configure-development-environment-for-pyodbc-python-development.md).
20-
* [Step 2: Create an SQL database for pyodbc Python development](step-2-create-a-sql-database-for-pyodbc-python-development.md).
21-
* [Step 3: Proof of concept connecting to SQL using pyodbc](step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md).
18+
- [Step 1: Configure development environment for pyodbc Python development](step-1-configure-development-environment-for-pyodbc-python-development.md)
19+
- [Step 2: Create an SQL database for pyodbc Python development](step-2-create-a-sql-database-for-pyodbc-python-development.md)
20+
- [Step 3: Proof of concept connecting to SQL using pyodbc](step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md)
2221

2322
## Documentation
2423

25-
For documentation, see [pyodbc documentation](https://mkleehammer.github.io/pyodbc/).
24+
- [pyodbc documentation](https://mkleehammer.github.io/pyodbc/)
2625

2726
## Support
2827

2928
PyODBC is community-supported software. Microsoft contributes to the pyODBC open-source community and is an active participant in the repository at [https://github.com/mkleehammer/pyodbc/](https://github.com/mkleehammer/pyodbc/). However, this software doesn't come with Microsoft support.
3029

31-
To get help, file an [issue in the pyODBC GitHub repository](https://github.com/mkleehammer/pyodbc/issues) or visit other Python community resources.
30+
> [!TIP]
31+
> To get help, file an [issue in the pyODBC GitHub repository](https://github.com/mkleehammer/pyodbc/issues) or visit other Python community resources.
3232
3333
## Community resources
3434

35-
* [Azure Python Developer Center](https://azure.microsoft.com/develop/python/)
36-
* [python.org Community](https://www.python.org/community/)
35+
- [Azure Python Developer Center](https://azure.microsoft.com/develop/python/)
36+
- [python.org Community](https://www.python.org/community/)
3737

3838
## Next steps
3939

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,79 @@
11
---
2-
title: "Step 1: Configure pyodbc Python environment"
3-
description: "Step 1 of this getting started guide involves installing Python, the Microsoft ODBC Driver for SQL Server, and pyODBC into your development environment."
2+
title: Step 1: Configure pyodbc environment
3+
description: Step 1 of this getting started guide involves installing Python, the Microsoft ODBC Driver for SQL Server, and pyODBC into your development environment.
44
author: David-Engel
55
ms.author: v-davidengel
6-
ms.date: 04/17/2023
6+
ms.date: 08/22/2023
77
ms.service: sql
88
ms.subservice: connectivity
9-
ms.topic: conceptual
9+
ms.topic: how-to
10+
# CustomerIntent: As a developer, I want to install the pymssql package so that I can connect to SQL with Python code.
1011
---
12+
1113
# Step 1: Configure development environment for pyodbc Python development
1214

13-
This article explains how to configure your development environment for pyodbc Python development.
15+
You need to configure your development environment with the prerequisites in order to develop an application using the pyodbc Python driver for SQL Server.
16+
17+
## Prerequisites
18+
19+
- Python 3
20+
- If you don't already have Python, install the **Python runtime** and **Python Package Index (PyPI) package manager** from [python.org](https://www.python.org/downloads/).
21+
- Prefer to not use your own environment? Open as a devcontainer using [GitHub Codespaces](https://github.com/features/codespaces).
22+
- [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/github/codespaces-blank?quickstart=1).
23+
24+
## Install the ODBC driver
25+
26+
This driver requires the host operating system to have the appropriate ODBC driver already installed.
27+
28+
### [Windows](#tab/windows)
29+
30+
1. Obtain and install the Microsoft ODBC driver for SQL Server on Windows:
1431

15-
## Windows
32+
- [Microsoft ODBC Driver for SQL Server on Windows](../../odbc/windows/system-requirements-installation-and-driver-files.md#installing-microsoft-odbc-driver-for-sql-server)
1633

17-
1. **Download Python installer**. If your machine doesn't have Python, install it. Go to the [**Python download page**](https://www.python.org/downloads/windows/) and download the appropriate installer. For example, if you are on a 64-bit machine, download the Python 3.10 (x64) installer.
18-
19-
2. **Install Python**. Once the installer is downloaded, do the following steps:
34+
1. Verify that you have installed the driver.
2035

21-
1. Double-click the file to start the installer.
22-
1. Select your language, and agree to the terms.
23-
1. Follow the instructions on the screen to install Python on your computer.
24-
1. You can verify that Python is installed by going to a command prompt and running `python -V`. You can also search for Python in the start menu.
36+
### [Linux](#tab/linux)
2537

26-
3. [**Install the Microsoft ODBC Driver for SQL Server on Windows**.](../../odbc/windows/system-requirements-installation-and-driver-files.md#installing-microsoft-odbc-driver-for-sql-server)
27-
28-
4. **Open cmd.exe as an administrator**.
38+
1. Obtain and install the Microsoft ODBC driver for SQL Server on Linux:
2939

30-
5. **Install pyodbc using pip - Python package manager**.
40+
- [Microsoft ODBC driver for SQL Server (Linux)](../../odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server.md)
3141

32-
```cmd
33-
pip install pyodbc
34-
```
42+
1. Verify that you have installed the driver.
43+
44+
### [macOS](#tab/macos)
45+
46+
1. Obtain and install the Microsoft ODBC driver for SQL Server on macOS:
47+
48+
- [Microsoft ODBC driver for SQL Server (macOS)](../../odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos.md)
49+
50+
1. Verify that you have installed the driver.
51+
52+
---
3553

36-
## Linux
54+
## Install the pyodbc package
3755

38-
Installing on Linux is similar. If the following instructions don't work, see the [pyODBC install instructions](https://github.com/mkleehammer/pyodbc/wiki/Install#installing-on-linux), which have more details for different Linux distributions.
56+
Get the [`pyodbc` package](https://pypi.org/project/pyodbc/) from PyPI.
3957

40-
1. **Open terminal**.
58+
1. Open a command prompt in an empty directory.
4159

42-
2. [**Install the Microsoft ODBC driver for SQL Server (Linux)**.](../../odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server.md)
60+
1. Install the [`pyodbc` package](https://pypi.org/project/pyodbc/).
4361

44-
3. **Install pyodbc**.
62+
```bash
63+
pip install pyodbc
64+
```
4565

46-
```bash
47-
sudo -H pip install pyodbc
48-
```
66+
## Check installed packages
4967

50-
## macOS
68+
You can use the PyPI command-line tool to verify that your intended packages are installed.
5169

52-
1. [**Install the Microsoft ODBC driver for SQL Server (macOS)**.](../../odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos.md)
70+
1. Check the list of installed packages with `pip list`.
5371

54-
2. **Install pyodbc** from a terminal using the [pyODBC install instructions](https://github.com/mkleehammer/pyodbc/wiki/Install#installing-on-macosx).
72+
```bash
73+
pip list
74+
```
5575

5676
## Next steps
5777

58-
[Create an SQL database for pyodbc](step-2-create-a-sql-database-for-pyodbc-python-development.md).
78+
> [!div class="nextstepaction"]
79+
> [Step 2: Create an SQL database for pyodbc Python development](step-2-create-a-sql-database-for-pyodbc-python-development.md)
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
---
2-
title: "Step 2: Create a SQL database for pyodbc"
3-
description: "Step 2 of this getting started guide involves creating a database in SQL Server or Azure SQL Database for the pyodbc sample."
2+
title: Step 2 - Create an SQL database for pyodbc
3+
description: Step 2 of this getting started guide involves creating a database in SQL Server or Azure SQL Database for this pyodbc sample.
44
author: David-Engel
55
ms.author: v-davidengel
6-
ms.date: "03/24/2022"
6+
ms.date: 08/22/2023
77
ms.service: sql
88
ms.subservice: connectivity
9-
ms.topic: conceptual
9+
ms.topic: how-to
10+
# CustomerIntent: As a developer, I want to create a SQL database so that I can connect to it with Python code.
1011
---
11-
# Step 2: Create a SQL database for pyodbc Python development
1212

13-
[!INCLUDE[CreateDatabase](../../../includes/createdatabase.md)]
13+
# Step 2: Create an SQL database for pyodbc Python development
14+
15+
Create an SQL database using either Azure SQL or SQL Server.
16+
17+
## Prerequisites
18+
19+
- Python 3
20+
- If you don't already have Python, install the **Python runtime** and **Python Package Index (PyPI) package manager** from [python.org](https://www.python.org/downloads/).
21+
- Prefer to not use your own environment? Open as a devcontainer using [GitHub Codespaces](https://github.com/features/codespaces).
22+
- [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/github/codespaces-blank?quickstart=1).
23+
- `pyodbc` package from PyPI.
24+
25+
## Create an SQL database
26+
27+
The samples in this next section only work with the AdventureWorks schema, on either Microsoft SQL Server or Azure SQL Database.
28+
29+
### [Azure SQL Database](#tab/azure-sql)
30+
31+
[Create an SQL database in minutes using the Azure portal](/azure/azure-sql/database/single-database-create-quickstart)
32+
33+
### [Microsoft SQL Server](#tab/sql-server)
34+
35+
[Microsoft SQL Server Samples on GitHub](https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks)
36+
37+
---
38+
39+
> [!IMPORTANT]
40+
> Don't forget to get the credentials for the database you create.
1441
1542
## Next steps
1643

17-
[Connect to SQL using pyodbc](step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md)
44+
> [!div class="nextstepaction"]
45+
> [Step 3: Proof of concept connecting to SQL using pyodbc](step-3-proof-of-concept-connecting-to-sql-using-pyodbc.md)

0 commit comments

Comments
 (0)