I am using a DynamoDB table named DMS in which I regularly update some values. Therefor I first save all values from an element in a variable called "response". The element has the Primary key 220 in my example. After this I save the value of the Attribute (in my case the element with the heading 522) in the variable CounterOnePlus and add 1. After this I try to update the given Attribute of the element in the column 522. Here is my Code:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DMS')
AverageValue=220
DeltaValue=522
response = table.get_item(
Key={
'Average': '%d' % AverageValue,
}
)
item = response['Item']
CounterOnePlus=int(item['%d' % DeltaValue])+1
print(CounterOnePlus)
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET 522 = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
}
)
The problem I am facing is, that I am not able to save the values. The reason for this is the Name of the heading, which is 522. Whenever I insert the value to a column with a string Header like "hallo", the value gets updated. Changing the value 522 to a string with the str(522) does not Change anything.
My error message is the following:
2
Traceback (most recent call last):
File "TestBoto3.py", line 20, in <module>
':val1': CounterOnePlus
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "522", near: "SET 522 ="
Any ideas on my issue? Thanks
Use ExpresssionAttributeNames (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html):
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET #DeltaValue = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
},
ExpressionAttributeNames= {
"#DeltaValue": str(DeltaValue) # 522
}
)