Insert Entity into Azure Table Storage

This document explains how to insert one or more entities into an Azure Table Storage table using the Azure SDKs.

Note: Azure Table Storage is a NoSQL key-value store that allows you to store large amounts of structured, non-relational data.

Prerequisites

Inserting a Single Entity

To insert a single entity, you typically create an object representing your entity and then use the appropriate client method to add it to the table.

Example using Python SDK


from azure.data.tables import TableServiceClient
from azure.data.tables._entity import Entity

# Replace with your connection string and table name
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MySampleTable"

# Create a TableServiceClient
table_service_client = TableServiceClient.from_connection_string(connection_string)

# Get a client for the specific table
table_client = table_service_client.get_table_client(table_name=table_name)

# Define the entity to insert
entity = {
    "PartitionKey": "Partition1",
    "RowKey": "Row1",
    "Description": "This is the first entity",
    "Quantity": 10,
    "Price": 25.50
}

try:
    # Insert the entity
    created_entity = table_client.upsert_entity(entity)
    print(f"Entity inserted successfully: {created_entity}")
except Exception as ex:
    print(f"An error occurred: {ex}")
            

Example using .NET SDK


using Azure.Data.Tables;
using System;

// Replace with your connection string and table name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MySampleTable";

// Create a TableServiceClient
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);

// Get a client for the specific table
TableClient tableClient = tableServiceClient.GetTableClient(tableName);

// Define the entity to insert (using a dynamic object for simplicity)
var entity = new
{
    PartitionKey = "Partition1",
    RowKey = "Row1",
    Description = "This is the first entity in .NET",
    Quantity = 5,
    Price = 19.99
};

try
{
    // Insert the entity
    await tableClient.UpsertEntityAsync(entity);
    Console.WriteLine("Entity inserted successfully.");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}
            

Inserting Multiple Entities

For better performance, you can insert multiple entities in a single batch operation. This is more efficient than making individual insert calls for each entity.

Important: Batch operations must contain entities from the same PartitionKey. You cannot mix entities from different partitions in a single batch.

Example using Python SDK for Batch Insert


from azure.data.tables import TableServiceClient
from azure.data.tables._entity import Entity

# Replace with your connection string and table name
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MySampleTable"

# Create a TableServiceClient
table_service_client = TableServiceClient.from_connection_string(connection_string)

# Get a client for the specific table
table_client = table_service_client.get_table_client(table_name=table_name)

# Define entities for the batch (all from the same PartitionKey)
entities_to_insert = [
    {
        "PartitionKey": "BatchPartition",
        "RowKey": "BatchRow1",
        "Name": "Item A",
        "Value": 100
    },
    {
        "PartitionKey": "BatchPartition",
        "RowKey": "BatchRow2",
        "Name": "Item B",
        "Value": 200
    },
    {
        "PartitionKey": "BatchPartition",
        "RowKey": "BatchRow3",
        "Name": "Item C",
        "Value": 150
    }
]

try:
    # Submit the batch insert operation
    results = table_client.submit_transaction(entities_to_insert)
    print("Batch insert operation submitted.")
    for result in results:
        print(f"  - {result}")
except Exception as ex:
    print(f"An error occurred during batch insert: {ex}")
            

Key Concepts