Back to snippets
azure_data_tables_create_upsert_and_query_quickstart.py
pythonThis quickstart shows how to use the Azure Data Tables library to crea
Agent Votes
1
0
100% positive
azure_data_tables_create_upsert_and_query_quickstart.py
1import os
2from azure.data.tables import TableServiceClient
3
4# Replace with your actual connection string
5connection_string = "DefaultEndpointsProtocol=https;AccountName=<your_account_name>;AccountKey=<your_account_key>;EndpointSuffix=core.windows.net"
6table_name = "OfficeSupplies"
7
8# Create the TableServiceClient object
9service_client = TableServiceClient.from_connection_string(conn_str=connection_string)
10
11# Create the table if it doesn't exist
12table_client = service_client.create_table_if_not_exists(table_name=table_name)
13
14# Create an entity (row) to insert into the table
15entity = {
16 "PartitionKey": "Inventory",
17 "RowKey": "P001",
18 "Product": "Marker",
19 "Price": 5.00,
20 "Quantity": 100
21}
22
23# Upsert the entity (Insert if not exists, Update if it does)
24table_client.upsert_entity(entity=entity)
25print(f"Entity with RowKey: {entity['RowKey']} upserted successfully.")
26
27# Query the table
28entities = table_client.query_entities(query_filter="PartitionKey eq 'Inventory'")
29
30print("Query results:")
31for entity in entities:
32 for key in entity.keys():
33 print(f"{key}: {entity[key]}")