问题
I am trying to achieve an update operation using my API method to update the content of the table.
I have following lambda code set up:
def lambda_handler(event, context):
param = event['queryStringParameters']['employeID']
name = event['queryStringParameters']['employeName']
dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
table = dynamodb.Table('api_demo_employe')
response = table.update_item(
Key = {
'employeID' : param
}
)
I need to figure out how can i set the UpdateExpression,ConditionExpression, ExpressionAttributeValues using my update variable.
Could any one help me out of this?
回答1:
It would be something like this:
response = table.update_item(TableName=table,
ReturnValues='UPDATED_NEW',
Key={'employeID': {'S': param}},
ExpressionAttributeValues={':f': {'S': update['some_param']},
':limit': {'N': '500'}},
UpdateExpression='SET some_field = :f',
ConditionExpression='some_other_field > :limit'
)
Of course this is just an example--your specific expressions will depend on what you are trying to do. And I am guessing at your data types. The 'S'
is for a string, but there are other options for other data types. You define the substitution variables in ExpressionAttributeValues
and then reference those in the UpdateExpression
and ConditionExpression
as needed.
Full details of all the options are available at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.update_item You'll want to review those options and look at the good examples there for how to do different things.
回答2:
I tried this out with help of @Shawn answer
response = table.update_item(
Key = {
'employeID' : param
},
ExpressionAttributeValues = {
':f': name
},
UpdateExpression = 'SET employeName = :f'
)
And it worked.
来源:https://stackoverflow.com/questions/63267747/updating-the-value-of-dynamodb-table-with-boto3-implemented-lambda-function