Is it possible to create a table in an Azure sql database using Python? I am pulling a list of things from an API and then want to push them to a table in my Azure sql db but can not find a tutorial or guide on how to do so. Googling for it led me to tutorials on how to pull data from my db. Thanks
Advertisement
Answer
If you are using Azure SQL database, you could follow the Azure official tutorial which @ Mohamed Elrashid provided for you: Azure SQL Database libraries for Python:
Example:
Create a SQL Database resource and restrict access to a range of IP addresses using a firewall rule.
from azure.common.client_factory import get_client_from_cli_profile from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.sql import SqlManagementClient RESOURCE_GROUP = 'YOUR_RESOURCE_GROUP_NAME' LOCATION = 'eastus' # example Azure availability zone, should match resource group SQL_SERVER = 'yourvirtualsqlserver' SQL_DB = 'YOUR_SQLDB_NAME' USERNAME = 'YOUR_USERNAME' PASSWORD = 'YOUR_PASSWORD' # create resource client resource_client = get_client_from_cli_profile(ResourceManagementClient) # create resource group resource_client.resource_groups.create_or_update(RESOURCE_GROUP, {'location': LOCATION}) sql_client = get_client_from_cli_profile(SqlManagementClient) # Create a SQL server server = sql_client.servers.create_or_update( RESOURCE_GROUP, SQL_SERVER, { 'location': LOCATION, 'version': '12.0', # Required for create 'administrator_login': USERNAME, # Required for create 'administrator_login_password': PASSWORD # Required for create } ) # Create a SQL database in the Basic tier database = sql_client.databases.create_or_update( RESOURCE_GROUP, SQL_SERVER, SQL_DB, { 'location': LOCATION, 'collation': 'SQL_Latin1_General_CP1_CI_AS', 'create_mode': 'default', 'requested_service_objective_name': 'Basic' } ) # Open access to this server for IPs firewall_rule = sql_client.firewall_rules.create_or_update( RESOURCE_GROUP, SQL_DB, "firewall_rule_name_123.123.123.123", "123.123.123.123", # Start ip range "167.220.0.235" # End ip range )
If you are using Azure Database for MySQL, please ref this Azure tutorial Python + Azure Database for MySQL.
Azure Database for MySQL and Python can be used together for data analysis – MySQL as database engine and Python as statistical tool. When dealing with large datasets that potentially exceed the memory of your machine it is recommended to push the data into database engine, where you can query the data in smaller digestible chunks.
In this article we will learn how to use Python to perform the following tasks:
- Create Azure Database for MySQL using azure python sdk
Hope this helps.