Getting Started with Azure SQL Management in Python

Laurent Mazuel tarafından
Son güncelleştirme tarihi: 12.1.2017
Katkıda Bulunanlar:
GitHub üzerinde düzenleyin

This sample shows how to manage SQL Server using the Azure Storage Resource Provider for Python.

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. We recommend using a virtual environment to run this example, but it's not mandatory. You can initialize a virtual environment this way:

    pip install virtualenv
    virtualenv mytestenv
    cd mytestenv
    source bin/activate
  3. Clone the repository.

    git clone
  4. Install the dependencies using pip.

    cd sql-database-python-manage
    pip install -r requirements.txt
  5. Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.

  6. Export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
  7. Run the sample.


What is doing?

The sample walks you through several SQL Server operations. It starts by setting up a ResourceManagementClient and SQLManagementClient objects using your subscription and credentials.

# Create the Resource Manager Client with an Application (service principal) token provider
subscription_id = os.environ.get(
    '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
resource_client = ResourceManagementClient(credentials, subscription_id)
sql_client = SqlManagementClient(credentials, subscription_id)

# You MIGHT need to add SQL as a valid provider for these credentials
# If so, this operation has to be done only once for each credential

# Create Resource group
resource_group_params = {'location':'westus'}
resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)

There are also a few supporting functions (print_item, print_metrics, and print_properties).

Create a SQL Server instance

server = sql_client.servers.create_or_update(
        'location': REGION,
        'version': '12.0', # Required for create
        'administrator_login': 'mysecretname', # Required for create
        'administrator_login_password': 'HusH_Sec4et' # Required for create

Get a server

server = sql_client.servers.get_by_resource_group(

List servers by resource group


List servers by subscription


List server usages

sql_client.servers.list_usages(GROUP_NAME, SERVER_NAME)

Create a database

async_db_create = sql_client.databases.create_or_update(
        'location': REGION
database = async_db_create.result() # Wait for completion and return created object

Get a database

database = sql_client.databases.get(

Get a list of databases

sql_client.databases.list_by_server(GROUP_NAME, SERVER_NAME)

Get a list of database usages

sql_client.databases.list_usages(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a database

sql_client.databases.delete(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a SQL Server instance

sql_client.servers.delete(GROUP_NAME, SERVER_NAME)

More information