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
Method
def update_row(self, table_name, row, condition, return_type=None, transaction_id=None)
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'] }