Skip to content

Commit 64ea7ab

Browse files
authored
Merge pull request MicrosoftDocs#208 from MicrosoftDocs/updates
SQL conceptual page
2 parents 99855f9 + 248a7e2 commit 64ea7ab

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

docs-ref-services/sql.md

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
---
22
title: Azure SQL Database libraries for Python
3-
description:
4-
keywords: Azure, Python, SDK, API, SQL, database , pyodbc
3+
description: Connect to Azure SQL database using the ODBC driver and pyodbc or manage Azure SQL instances with the management API.
54
author: lisawong19
65
ms.author: liwong
7-
manager: douge
8-
ms.date: 07/11/2017
9-
ms.topic: article
10-
ms.prod: azure
11-
ms.technology: azure
6+
manager: routlaw
7+
ms.date: 01/09/2018
8+
ms.topic: reference
129
ms.devlang: python
1310
ms.service: sql-database
1411
---
@@ -17,44 +14,60 @@ ms.service: sql-database
1714

1815
## Overview
1916

20-
Work with data stored in [Azure SQL Database](/azure/sql-database/sql-database-technical-overview) from Python with the Microsoft ODBC driver and pyodbc.
17+
Work with data stored in [Azure SQL Database](/azure/sql-database/sql-database-technical-overview) from Python with the pyodbc [ODBC database driver](https://github.com/mkleehammer/pyodbc/wiki/Drivers-and-Driver-Managers). View our [quickstart](https://docs.microsoft.com/azure/sql-database/sql-database-connect-query-python) on connecting to an Azure SQL database and using Transact-SQL statements to query data and getting started [sample](https://github.com/mkleehammer/pyodbc/wiki/Getting-started) with pyodbc.
2118

22-
## Client ODBC driver and pyodbc
19+
## Install ODBC driver and pyodbc
2320

2421
```bash
2522
pip install pyodbc
2623
```
27-
More details about installing the python and database communication libraries can be found [here](https://docs.microsoft.com/azure/sql-database/sql-database-connect-query-python#install-the-python-and-database-communication-libraries).
24+
More [details](https://docs.microsoft.com/azure/sql-database/sql-database-connect-query-python#install-the-python-and-database-communication-libraries) about installing the python and database communication libraries.
2825

29-
### Example
26+
## Connect and execute a SQL query
3027

31-
Connect to a SQL database and select all records in a table.
28+
### Connect to a SQL database
3229

3330
```python
34-
import pyodbc
31+
import pyodbc
3532

36-
SERVER = 'YOUR_SERVER_NAME.database.windows.net'
37-
DATABASE = 'YOUR_DATABASE_NAME'
38-
USERNAME = 'YOUR_DB_USERNAME'
39-
PASSWORD = 'YOUR_DB_PASSWORD'
33+
server = 'your_server.database.windows.net'
34+
database = 'your_database'
35+
username = 'your_username'
36+
password = 'your_password'
37+
driver= '{ODBC Driver 13 for SQL Server}'
4038

41-
DRIVER= '{ODBC Driver 13 for SQL Server}'
42-
cnxn = pyodbc.connect('DRIVER=' + DRIVER + ';PORT=1433;SERVER=' + SERVER +
43-
';PORT=1443;DATABASE=' + DATABASE + ';UID=' + USERNAME + ';PWD=' + PASSWORD)
39+
cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password)
4440
cursor = cnxn.cursor()
45-
selectsql = "SELECT * FROM SALES" # SALES is an example table name
46-
cursor.execute(selectsql)
4741
```
4842

49-
## Management API
43+
### Execute a SQL query
44+
45+
```python
46+
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
47+
row = cursor.fetchone()
48+
while row:
49+
print (str(row[0]) + " " + str(row[1]))
50+
row = cursor.fetchone()
51+
```
52+
53+
> [!div class="nextstepaction"]
54+
> [pyodbc sample](https://github.com/mkleehammer/pyodbc/wiki/Getting-started)
55+
56+
## Connecting to ORMs
57+
58+
pyodbc works with other ORMs such as [SQLAlchemy](http://docs.sqlalchemy.org/en/latest/dialects/mssql.html?highlight=pyodbc#module-sqlalchemy.dialects.mssql.pyodbc) and [Django](https://github.com/lionheart/django-pyodbc/).
59+
60+
## [Management API](/python/api/overview/azure/sql/managementlibrary)
5061

5162
Create and manage Azure SQL Database resources in your subscription with the management API.
5263

5364
```bash
65+
pip install azure-common
5466
pip install azure-mgmt-sql
67+
pip install azure-mgmt-resource
5568
```
5669

57-
### Example
70+
## Example
5871

5972
Create a SQL Database resource and restrict access to a range of IP addresses using a firewall rule.
6073

@@ -63,6 +76,13 @@ RESOURCE_GROUP = 'YOUR_RESOURCE_GROUP_NAME'
6376
LOCATION = 'eastus' # example Azure availability zone, should match resource group
6477
SQL_DB = 'YOUR_SQLDB_NAME'
6578

79+
# create resource client
80+
resource_client = get_client_from_cli_profile(ResourceManagementClient)
81+
# create resource group
82+
resource_client.resource_groups.create_or_update(RESOURCE_GROUP, {'location': LOCATION})
83+
84+
sql_client = get_client_from_cli_profile(SqlManagementClient)
85+
6686
# Create a SQL server
6787
server = sql_client.servers.create_or_update(
6888
RESOURCE_GROUP,
@@ -87,12 +107,3 @@ firewall_rule = sql_client.firewall_rules.create_or_update(
87107
> [!div class="nextstepaction"]
88108
> [Explore the Management APIs](/python/api/overview/azure/sql/managementlibrary)
89109
90-
## Samples
91-
92-
* [Create and manage SQL databases][1]
93-
* [Use Python to connect and query data][2]
94-
95-
[1]: https://github.com/Azure-Samples/sql-database-python-manage
96-
[2]: https://docs.microsoft.com/azure/sql-database/sql-database-connect-query-python
97-
98-
View the [complete list](https://azure.microsoft.com/resources/samples/?platform=python&term=SQL) of Azure SQL database samples.

0 commit comments

Comments
 (0)