Modifier

Partager via


Bibliothèques Azure MySQL/PostgreSQL pour PythonAzure MySQL/PostgreSQL libraries for Python

MySQLMySQL

Utilisez des ressources et des données stockées dans la base de données Azure MySQL à partir de Python avec le Gestionnaire MySQL et pyodbc.Work with resources and data stored in Azure MySQL Database from python with the MySQL manager and pyodbc.

Pilote ODBC du client et pyodbcClient ODBC driver and pyodbc

Nous recommandons le pilote ODBC Microsoft comme bibliothèque client pour accéder à la base de données Azure pour MySQL.The recommended client library for accessing Azure Database for MySQL is the Microsoft ODBC driver. Utilisez le pilote ODBC pour vous connecter à la base de données et exécuter les instructions SQL directement.Use the ODBC driver to connect to the database and execute SQL statements directly.

ExemplesExample

Connectez-vous à la base de données Azure pour MySQL et sélectionnez tous les enregistrements dans le tableau des ventes.Connect to a Azure Database for MySQL and select all records in the sales table. Il est possible d’obtenir la chaîne de connexion ODBC de la base de données à partir du portail Azure.You can get the ODBC connection string for the database from the Azure Portal.

SERVER = 'YOUR_SEVER_NAME' + '.mysql.database.azure.com'
DATABASE = 'YOUR_DATABASE_NAME'
USERNAME = 'YOUR_MYSQL_USERNAME'
PASSWORD = 'YOUR_MYSQL_PASSWORD'

DRIVER = '{MySQL ODBC 5.3 UNICODE Driver}'
cnxn = pyodbc.connect(
    'DRIVER=' + DRIVER + ';PORT=3306;SERVER=' + SERVER + ';PORT=3306;DATABASE=' + DATABASE + ';UID=' + USERNAME + ';PWD=' + PASSWORD)
cursor = cnxn.cursor()
selectsql = "SELECT * FROM SALES"  # SALES is an example table name
cursor.execute(selectsql)

API de gestionManagement API

Créez et gérez des ressources MySQL dans votre abonnement avec l’API de gestion.Create and manage MySQL resources in your subscription with the management API.

Configuration requiseRequirements

Vous devez installer les bibliothèques de gestion MySQL pour Python.You must install the MySQL management libraries for Python.

pip install azure-mgmt-rdbms

Veuillez consulter la page sur l’l’authentification du Kit de développement logiciel (SDK) Pythonpour en savoir plus sur l’obtention des informations d’identification pour l’authentification avec le client de gestion.Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.

ExemplesExample

Créez une ressource de base de données MySQL 5.7 et restreignez l’accès à une plage d’adresses IP à l’aide d’une règle de pare-feu.Create a MySQL 5.7 Database resource and restrict access to a range of IP addresses using a firewall rule.


from azure.mgmt.rdbms.mysql import MySQLManagementClient
from azure.mgmt.rdbms.mysql.models import ServerForCreate, ServerPropertiesForDefaultCreate, ServerVersion

SUBSCRIPTION_ID = "YOUR_AZURE_SUBSCRIPTION_ID"
RESOURCE_GROUP = "YOUR_AZURE_RESOURCE-GROUP_WITH_POSTGRES"
MYSQL_SERVER = "YOUR_DESIRED_MYSQL_SERVER_NAME"
MYSQL_ADMIN_USER = "YOUR_MYSQL_ADMIN_USERNAME"
MYSQL_ADMIN_PASSWORD = "YOUR_MYSQL_ADMIN_PASSOWRD"
LOCATION = "westus"  # example Azure availability zone, should match resource group


client = MySQLManagementClient(credentials=creds,
    subscription_id=SUBSCRIPTION_ID)

server_creation_poller = client.servers.create_or_update(
    resource_group_name=RESOURCE_GROUP,
    server_name=MYSQL_SERVER,
    ServerForCreate(
        ServerPropertiesForDefaultCreate(
            administrator_login=MYSQL_ADMIN_USER,
            administrator_login_password=MYSQL_ADMIN_PASSWORD,
            version=ServerVersion.five_full_stop_seven
        ),
        location=LOCATION
    )
)

server = server_creation_poller.result()

# Open access to this server for IPs
rule_creation_poller = client.firewall_rules.create_or_update(
    RESOURCE_GROUP
    MYSQL_SERVER,
    "some_custom_ip_range_whitelist_rule_name",  # Custom firewall rule name
    "123.123.123.123",  # Start ip range
    "167.220.0.235"  # End ip range
)

firewall_rule = rule_creation_poller.result()

PostgreSQLPostgreSQL

Utilisez le pilote ODBC et pyodbc pour vous connecter à la base de données et exécuter les instructions SQL directement.Use the ODBC driver and pyodbc to connect to the database and execute SQL statements directly.

En savoir plus sur les bases de données Azure pour PostgreSQL.Learn more about Azure Database for PostgreSQL.

Pilote ODBC du client et pyodbcClient ODBC driver and pyodbc

Nous recommandons pyodbc et le pilote ODBC Microsoft comme bibliothèque cliente pour accéder à Azure Database pour PostgreSQL.The recommended client library for accessing Azure Database for PostgreSQL is the Microsoft ODBC driver and pyodbc.

ExemplesExample

Connectez-vous à la base de données Azure pour PostgreSQL et sélectionnez tous les enregistrements dans le tableau SALES.Connect to a Azure Database for PostgreSQL and select all records in the SALES table. Il est possible d’obtenir la chaîne de connexion ODBC de la base de données à partir du portail Azure.You can get the ODBC connection string for the database from the Azure Portal.

import pyodbc

SERVER = 'YOUR_SERVER_NAME.postgres.database.azure.com'
DATABASE = 'YOUR_DB_NAME'
USERNAME = 'YOUR_USERNAME'
PASSWORD = 'YOUR_PASSWORD'

DRIVER = '{PostgreSQL ODBC Driver}'
cnxn = pyodbc.connect(
    'DRIVER=' + DRIVER + ';PORT=5432;SERVER=' + SERVER +
    ';PORT=5432;DATABASE=' + DATABASE + ';UID=' + USERNAME +
    ';PWD=' + PASSWORD)
cursor = cnxn.cursor()
selectsql = "SELECT * FROM SALES" # SALES is an example table name
cursor.execute(selectsql)

API de gestionManagement API

Configuration requiseRequirements

Vous devez installer les bibliothèques de gestion PostgreSQL pour Python.You must install the PostgreSQL management libraries for Python.

pip install azure-mgmt-rdbms

Veuillez consulter la page sur l’l’authentification du Kit de développement logiciel (SDK) Pythonpour en savoir plus sur l’obtention des informations d’identification pour l’authentification avec le client de gestion.Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.

ExemplesExample

Dans cet exemple, nous allons créer une base de données Postgres sur notre serveur Postgres existant.In this example we will create a new Postgres database on our existing Postgres server.

from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient

SUBSCRIPTION_ID = "YOUR_AZURE_SUBSCRIPTION_ID"
RESOURCE_GROUP = "YOUR_AZURE_RESOURCE_GROUP_WITH_POSTGRES"
POSTGRES_SERVER = "YOUR_POSTGRES_SERVER_NAME"
DB_NAME = "YOUR_DESIRED_DATABASE_NAME"

client = PostgreSQLManagementClient(credentials, SUBSCRIPTION_ID)

db_creation_poller = client.databases.create_or_update(
    resource_group_name=RESOURCE_GROUP,
    server_name=POSTGRES_SERVER, database_name=DB_NAME)
db = db_creation_poller.result()