All Products
Search
Document Center

Tablestore:Write a single row of data

Last Updated:Mar 31, 2026

Use the Python SDK to write a single row of data to a Tablestore data table.

Prerequisites

Initialize a Tablestore client

Method

put_row(self, table_name, row, condition=None, 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 write. This parameter includes the following:

  • primary_key (Required) List[Tuple]: The names and values of the primary key columns.

    • Primary key column data types are STRING, INTEGER, and BINARY.

    • The number and data types of the primary key columns must match those defined for the data table.

    • If a primary key column is an auto-increment column, set its value to a placeholder. For more information, see Auto-increment primary key columns.

  • attribute_columns (Required) List[Tuple]: The names, values, and data versions of the attribute columns.

    • Attribute column data types are STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN.

    • The data version is a timestamp. By default, Tablestore automatically generates a version, but you can also specify a custom one. For more information, see Data versions and data lifecycle.

condition (Optional)

Condition

The condition for the write operation. For more information, see Conditional update.

return_type (Optional)

ReturnType

The type of data to return.

  • RT_NONE (Default): No data is returned.

  • RT_PK: Returns the primary key columns. This is useful for retrieving the generated value of an auto-increment primary key column.

transaction_id (Optional)

str

The unique ID for a local transaction. For more information, see Local transactions.

Examples

The following example writes a row with the primary key value row1 to the test_table table.

try:
    # Construct the primary key and attribute columns.
    primary_key = [('id', 'row1')]
    attribute_columns = []

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

    # Call the put_row method to write the row.
    consumed, return_row = client.put_row('test_table', row)
    print('* Read CU Cost: %s' % consumed.read)
    print('* Write CU Cost: %s' % consumed.write)
except Exception as e:
    print("Put row failed with error: %s" % e)
  • To add an attribute column:

    attribute_columns = [('col1','val1')]
  • To specify a data version, assign a version to each attribute column.

    attribute_columns = [('col1','val1', int(time.time() * 1000))]

References

Batch update data