All Products
Search
Document Center

Tablestore:Update a single row

Last Updated:Mar 31, 2026

Use the Python SDK to update a single row in a Tablestore data table. This method supports updating attribute column values, adding attribute columns, deleting a specific version of an attribute column, or deleting an entire attribute column.

Prerequisites

Initialize a Tablestore client

Method

def update_row(self, table_name, row, condition, return_type=None, transaction_id=None)

Parameters

Parameter

Type

Description

table_name (Required)

str

The name of the data table.

row (Required)

Row

The row to update. This parameter includes the following:

  • primary_key (Required)List[Tuple]: The primary key, which includes the name and value of each primary key column.

    • The data type of a primary key column can be STRING, INTEGER, or BINARY.

    • The number and data types of the primary key columns must match the table's primary key schema.

  • attribute_columns (Required)dict: The attribute columns to update and the operations to perform.

condition (Required)

Condition

The update condition. For more information, see Conditional update.

return_type (Optional)

ReturnType

The return type.

  • RT_NONE (Default): No data is returned.

  • RT_PK: Returns the primary key columns.

transaction_id (Optional)

str

The ID of the local transaction. For more information, see Local transactions.

Examples

The following example updates a row in the test_table table where the primary key value is row1, changing the value of col1 to changed_val1.

try:
    # Construct the primary key.
    primary_key = [('id', 'row1')]
    # Construct the attribute columns to update.
    attribute_columns = {
        'PUT': [('col1', 'changed_val1')]
    }

    # Construct the row to update.
    row = Row(primary_key, attribute_columns)

    # Call the update_row method to update the row.
    # You must specify a condition to update a row.
    # RowExistenceExpectation.IGNORE means the system does not check whether the row exists before the update.
    consumed, return_row = client.update_row('test_table', row, Condition(RowExistenceExpectation.IGNORE))
    print('Read CU Cost: %s' % consumed.read)
    print('Write CU Cost: %s' % consumed.write)
except Exception as e:
    print("Update row failed with error: %s" % e)

Perform the following row operations as needed:

  • Add an attribute column.

    attribute_columns = {
        'PUT': [('col2', 'val2')]
    }
  • Set the version number for an attribute column.

    attribute_columns = {
        'PUT': [('col2', 'val2', int(time.time() * 1000))]
    }
  • Delete a specific version of an attribute column.

    attribute_columns = {
        'DELETE': [('col2', None, 1747893563831)]
    }
  • Delete an entire attribute column.

    attribute_columns = {
        'DELETE_ALL': ['col2']
    }

References