DynamoDB-UpdateItem.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import boto3
  2. # use Resource
  3. def increase_rating(keya,dynamodb=None):
  4. if not dynamodb:
  5. dynamodb = boto3.resource('dynamodb')
  6. table = dynamodb.Table('hehe')
  7. response = table.update_item(
  8. Key={'name': keya},
  9. UpdateExpression="set alias=:val",
  10. ExpressionAttributeValues={
  11. ':val': 'hehe'},
  12. ReturnValues="UPDATED_NEW")
  13. return response
  14. # use Client
  15. def updata_ddb(name,msg,alias):
  16. table = boto3.client('dynamodb')
  17. table.update_item(TableName='hehe',
  18. Key={'name': {'S':name}},
  19. UpdateExpression='SET name = :name, alias = :alias',
  20. ExpressionAttributeValues={
  21. ':name': {
  22. 'S': msg},
  23. ':alias':{
  24. 'S': alias}
  25. })
  26. def updata_ddbone(age):
  27. client = boto3.client('dynamodb')
  28. response = client.update_item(
  29. TableName='age',
  30. Key={
  31. 'key': {
  32. 'S': "name"
  33. }
  34. },
  35. AttributeUpdates={
  36. 'age': {
  37. 'Value': {
  38. 'S': age
  39. }
  40. }
  41. }
  42. )
  43. if __name__ == '__main__':
  44. update_response = increase_rating("cxy")
  45. print("Update movie succeeded:")